首页 新闻 论坛 群组 Blog 文档 下载 读书 Tag 网摘 搜索 .NET Java 游戏 视频 人才 外包 培训 数据库 书店 程序员
中国软件网
欢迎您:游客 | 登录 注册 帮助
  • 挺有意思的传值问题,考查基本功~~~ [已结帖,结帖人:fuqiang229]
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fuqiang229
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    • 结帖率:
    发表于:2008-08-23 13:11:48 楼主
    //一个小小的关于传值的问题,以前没留意过这个问题,还挺有意思的.
    //这里有个一小程序,作用就是copy,把a数组copy到b数组,
    //用这个程序有点笨,但它只是我另一个问题的抽象......
    //这样一定是不对的,copy()方法中的b只是一个局部变量,值根本传不回去!,
    //我的问题就是如何改动copy()方法,保持main()的形式不变,还能成功copy成功.千万不要说改成b = copy(a)

    的形式,我说过了它只是我另一个问题的抽象;

    public class Test{
    public static void main(String[] args){
    // a变量中的值的个数不能确定,所以b没办法new一个int[],
    // 就是你new 了一个够大的 new int[100],后面也还是同一个问题
    int[] a = new int[]{1,2,3,4,5};
    int[] b = null;
    copy(a , b);
    for(int i = 0 ; i < 5 ; i++){
    System.out.println(b[i]);
    }

    }
    // 千万不要说改成b = copy(a)的形式
    public static void copy(int[] a, int[] b){
    b = new int[]{0,0,0,0,0};
    System.arraycopy(a,0 , b ,0 , a.length);

    }

    }
    50  修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fuqiang229
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-23 13:29:171楼 得分:0
    没人回答呀,分少了吗?呵呵.我看了一下 java 的API,System.arraycopy(a,b);和我的问题很像
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fuqiang229
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-23 13:34:232楼 得分:0
    恕小弟才疏学浅看不懂 我就想这个System.arraycopy()是怎么实现的呢?
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fuqiang229
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-23 13:34:453楼 得分:0
    /**
        * Copies an array from the specified source array, beginning at the
        * specified position, to the specified position of the destination array.
        * A subsequence of array components are copied from the source
        * array referenced by <code>src </code> to the destination array
        * referenced by <code>dest </code>. The number of components copied is
        * equal to the <code>length </code> argument. The components at
        * positions <code>srcPos </code> through
        * <code>srcPos+length-1 </code> in the source array are copied into
        * positions <code>destPos </code> through
        * <code>destPos+length-1 </code>, respectively, of the destination
        * array.
        * <p>
        * If the <code>src </code> and <code>dest </code> arguments refer to the
        * same array object, then the copying is performed as if the
        * components at positions <code>srcPos </code> through
        * <code>srcPos+length-1 </code> were first copied to a temporary
        * array with <code>length </code> components and then the contents of
        * the temporary array were copied into positions
        * <code>destPos </code> through <code>destPos+length-1 </code> of the
        * destination array.
        * <p>
        * If <code>dest </code> is <code>null </code>, then a
        * <code>NullPointerException </code> is thrown.
        * <p>
        * If <code>src </code> is <code>null </code>, then a
        * <code>NullPointerException </code> is thrown and the destination
        * array is not modified.
        * <p>
        * Otherwise, if any of the following is true, an
        * <code>ArrayStoreException </code> is thrown and the destination is
        * not modified:
        * <ul>
        * <li>The <code>src </code> argument refers to an object that is not an
        *    array.
        * <li>The <code>dest </code> argument refers to an object that is not an
        *    array.
        * <li>The <code>src </code> argument and <code>dest </code> argument refer
        *    to arrays whose component types are different primitive types.
        * <li>The <code>src </code> argument refers to an array with a primitive
        *    component type and the <code>dest </code> argument refers to an array
        *    with a reference component type.
        * <li>The <code>src </code> argument refers to an array with a reference
        *    component type and the <code>dest </code> argument refers to an array
        *    with a primitive component type.
        * </ul>
        * <p>
        * Otherwise, if any of the following is true, an
        * <code>IndexOutOfBoundsException </code> is
        * thrown and the destination is not modified:
        * <ul>
        * <li>The <code>srcPos </code> argument is negative.
        * <li>The <code>destPos </code> argument is negative.
        * <li>The <code>length </code> argument is negative.
        * <li> <code>srcPos+length </code> is greater than
        *    <code>src.length </code>, the length of the source array.
        * <li> <code>destPos+length </code> is greater than
        *    <code>dest.length </code>, the length of the destination array.
        * </ul>
        * <p>
        * Otherwise, if any actual component of the source array from
        * position <code>srcPos </code> through
        * <code>srcPos+length-1 </code> cannot be converted to the component
        * type of the destination array by assignment conversion, an
        * <code>ArrayStoreException </code> is thrown. In this case, let
        * <b> <i>k </i> </b> be the smallest nonnegative integer less than
        * length such that <code>src[srcPos+ </code> <i>k </i> <code>] </code>
        * cannot be converted to the component type of the destination
        * array; when the exception is thrown, source array components from
        * positions <code>srcPos </code> through
        * <code>srcPos+ </code> <i>k </i> <code>-1 </code>
        * will already have been copied to destination array positions
        * <code>destPos </code> through
        * <code>destPos+ </code> <i>k </I> <code>-1 </code> and no other
        * positions of the destination array will have been modified.
        * (Because of the restrictions already itemized, this
        * paragraph effectively applies only to the situation where both
        * arrays have component types that are reference types.)
        *
        * @param      src      the source array.
        * @param      srcPos  starting position in the source array.
        * @param      dest    the destination array.
        * @param      destPos  starting position in the destination data.
        * @param      length  the number of array elements to be copied.
        * @exception  IndexOutOfBoundsException  if copying would cause
        *              access of data outside array bounds.
        * @exception  ArrayStoreException  if an element in the <code>src </code>
        *              array could not be stored into the <code>dest </code> array
        *              because of a type mismatch.
        * @exception  NullPointerException if either <code>src </code> or
        *              <code>dest </code> is <code>null </code>.
        */
        public static native void arraycopy(Object src,  int  srcPos,
                                            Object dest, int destPos,
                                            int length);
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • SARA520
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-23 13:44:544楼 得分:0
    从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。从 src 引用的源数组到 dest 引用的目标数组,数组组件的一个子序列被复制下来。被复制的组件的编号等于 length 参数。源数组中位置在 srcPos 到 srcPos+length-1 之间的组件被分别复制到目标数组中的 destPos 到 destPos+length-1 位置。
    如果参数 src 和 dest 引用相同的数组对象,则复制的执行过程就好像首先将 srcPos 到 srcPos+length-1 位置的组件复制到一个带有 length 组件的临时数组,然后再将此临时数组的内容复制到目标数组的 destPos 到 destPos+length-1 位置一样。

    If 如果 dest 为 null,则抛出 NullPointerException 异常。

    如果 src 为 null, 则抛出 NullPointerException 异常,并且不会修改目标数组。

    否则,只要下列任何情况为真,则抛出 ArrayStoreException 异常并且不会修改目标数组:

    src 参数指的是非数组对象。
    dest 参数指的是非数组对象。
    src 参数和 dest 参数指的是那些其组件类型为不同基本类型的数组。
    src 参数指的是具有基本组件类型的数组且 dest 参数指的是具有引用组件类型的数组。
    src 参数指的是具有引用组件类型的数组且 dest 参数指的是具有基本组件类型的数组。
    否则,只要下列任何情况为真,则抛出 IndexOutOfBoundsException 异常,并且不会修改目标数组:

    srcPos 参数为负。
    destPos 参数为负。
    length 参数为负。
    srcPos+length 大于 src.length,即源数组的长度。
    destPos+length 大于 dest.length,即目标数组的长度。
    否则,如果源数组中 srcPos 到 srcPos+length-1 位置上的实际组件通过分配转换并不能转换成目标数组的组件类型,则抛出 ArrayStoreException 异常。在这种情况下,将 k 设置为比长度小的最小非负整数,这样就无法将 src[srcPos+k] 转换为目标数组的组件类型;当抛出异常时,从 srcPos 到 srcPos+k-1 位置上的源数组组件已经被复制到目标数组中的 destPos 到 destPos+k-1 位置,而目标数组中的其他位置不会被修改。(因为已经详细说明过的那些限制,只能将此段落有效地应用于两个数组都有引用类型的组件类型的情况。)


    参数:
    src - 源数组。
    srcPos - 源数组中的起始位置。
    dest - 目标数组。
    destPos - 目标数据中的起始位置。
    length - 要复制的数组元素的数量。
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • SARA520
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-23 13:45:535楼 得分:0
    怎么实现好像到比较底层的东西了
    看不到代码
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • SARA520
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-23 13:47:516楼 得分:0
    引用楼主 fuqiang229 的帖子:
    // a变量中的值的个数不能确定,所以b没办法new一个int[],
    // 就是你new 了一个够大的 new int[100],后面也还是同一个问题


    你在a个数确定后
    int[] b = new int[a.length];不行么???
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fosjos
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-23 13:48:457楼 得分:0
    native方法应该没有开源的代码,如果想知道就问sun去
    Java code
    int[] a = new int[]{1,2,3,4,5}; int[] b = new int[a.length]; System.arraycopy(a, 0, b, 0, a.length);

    方法只能改变Object参数的属性,不能改变参数本身
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • SARA520
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-23 13:51:068楼 得分:0
    public class Test {

    public static void main(String[] args){
    // a变量中的值的个数不能确定,所以b没办法new一个int[],
    // 就是你new 了一个够大的 new int[100],后面也还是同一个问题
    int[] a = new int[]{1,2,3,4,5};
    int[] b = new int[a.length];

    copy(a , b);
    for(int i = 0 ; i < 5 ; i++){
    System.out.println(b[i]);
    }
    }

    public static void copy(int[] a, int[] b){
    System.arraycopy(a,0 , b ,0 , a.length);

    }


    }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • justinavril
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-23 14:24:389楼 得分:0
    Java code
    public class Test{ public static void main(String[] args){ int[] a = new int[]{1,2,3,4,5}; int[] b = new int[a.length]; //应该初始化成和a长度一样的数组 copy(a, b); for(int i=0; i<b.length; i++){ System.out.println(b[i]); } } public static int[] copy(int[] a, int[] b){ for (int i=0; i<a.length; i++) b[i] = a[i]; return b; } }
     
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fuqiang229
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-23 14:38:1010楼 得分:0
    7,8,9楼,都说了new 是不行的,不拿出原问题你们都不死心~~~~~~~~~~~~~~`

    /*
    * 这个方法是把数据中保存的字节数据转化为字符数据
    * 更重要的一点是如果字节中保存有与cdata段结束定界符冲突的"]]>",要把这三个字节数据转化为"]] >"字符
    * 而数据中出现的"]] >"要转化为"]] >>"五个字符,使数据可以再次读取
    */
    public int transformChar(byte[] bData , char[] cData){
    // 这个变量为int型,4个字节,是检查字符串合法性的队列
    int checkTrack = 0;
    // 这个变量保存char[]的下标
    int j = 0;
    // 这个变量保存byte[]的下标
    int i = 0;
    // 这个变量保存字符数组
    char[] cCopy = new char[200];
    for( ; i < bData.length ; i++,j++){
    // 保存取出的字节
    byte get = bData[i];
    // 左一位舍去,右三位左移一位,右一位放字节
    checkTrack < <= 8;
    checkTrack |= get;
    // "]] >" == 93 93 32 62 == 5d 5d 20 3e
    // if(checkTrack右边三位是]]>)
    if((checkTrack & 0x00ffffff) == 0x005d5d3e){
    cCopy[j] = ' ';
    j++;
    }
    // if(checkTrack四位是]] >)
    if((checkTrack & 0xffffffff) == 0x5d5d5d5d){
    cCopy[j] = '>';
    j++;
    }
    cCopy[j] = (char)get;
    }
    char[] cTempCopy = new char[j];
    System.arraycopy(cCopy , 0 , cTempCopy , 0 , j);
    cData = cTempCopy;
    return cData.length;
    }
    }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fuqiang229
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-23 14:39:5611楼 得分:0
    引用 6 楼 SARA520 的回复:
    引用楼主 fuqiang229 的帖子:
    // a变量中的值的个数不能确定,所以b没办法new一个int[],
    // 就是你new 了一个够大的 new int[100],后面也还是同一个问题


    你在a个数确定后
    int[] b = new int[a.length];不行么???

    你也看的出来,char数组中有多少数据是在方法中在能知道的.
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fuqiang229
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-23 14:41:0412楼 得分:0
    不知道用hascode行不行呵呵..........可惜我不会.
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • SARA520
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-23 15:32:3013楼 得分:0
    那是你表达的问题
    不是我们说的那些

    实在不知道你要表达什么意思了
    没懂
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • justinavril
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-23 15:49:2114楼 得分:0
    引用 13 楼 SARA520 的回复:
    那是你表达的问题
    不是我们说的那些

    实在不知道你要表达什么意思了
    没懂

    同感...我看到你的代码和描述  立刻就晕了 
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • ldy214
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-23 16:03:1315楼 得分:0
    周末 上来 逛逛
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • AWUSOFT
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-23 19:05:1316楼 得分:0
    怎么可能完成啊.....
    两个不同的引用变量.如果你在外边创建好数组对象,然后传进来还差不多吧.
    外边的是null,你的形参传也是null,然后你形参创建了一个对象,实参并没有改变啊.也就是程序外边的b还是null;
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • KOOK_OKKO
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-23 21:50:1417楼 得分:0
    Java code
    public class Test{ public static void main(String[] args){ // a变量中的值的个数不能确定,所以b没办法new一个int[], // 就是你new 了一个够大的 new int[100],后面也还是同一个问题 int[] a = new int[]{1,2,3,4,5}; int[] b = new int[]{0,0,0,0,0}; copy(a , b); for(int i = 0 ; i < 5 ; i++){ System.out.println(b[i]); } } // 千万不要说改成b = copy(a)的形式 public static void copy(int[] a, int[] b){ //b = new int[]{0,0,0,0,0}; System.arraycopy(a,0 , b ,0 , a.length); } }


    把数组b的定义拿出来,不然就成了在函数内新建一个数组了
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • g1092407
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-23 21:54:1318楼 得分:0
    引用 6 楼 SARA520 的回复:
    引用楼主 fuqiang229 的帖子:
    // a变量中的值的个数不能确定,所以b没办法new一个int[],
    // 就是你new 了一个够大的 new int[100],后面也还是同一个问题


    你在a个数确定后
    int[] b = new int[a.length];不行么???


    对啊 这么做不就可以了?
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • hjfwxy
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-23 23:09:3819楼 得分:0
    public class Test{
    public static void main(String[] args){
    // a变量中的值的个数不能确定,所以b没办法new一个int[],
    // 就是你new 了一个够大的 new int[100],后面也还是同一个问题
    int[] a = new int[]{1,2,3,4,5};
    int[] b = null;
    copy(a , b);
    for(int i = 0 ; i < 5 ; i++){
    System.out.println(b[i]);
    }

    }
    // 千万不要说改成b = copy(a)的形式
    public static int[] copy(int[] a, int[] b){
    //扩展数组为a的2倍受欢迎
    int[] b=new int[a.leng*2];
    //b = new int[]{0,0,0,0,0};
    System.arraycopy(a,0 , b ,0 , a.length);
      b=a;
      return b;
    }

    }
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • skycncomp
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-23 23:32:5920楼 得分:0
    接分
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • xxc3303
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-24 09:34:2221楼 得分:0
    把copy方法改成int[]的啊,然后return不就得了么
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • shengli_liao
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-24 09:44:2222楼 得分:0
    // a变量中的值的个数不能确定,所以b没办法new一个int[],
    // 就是你new 了一个够大的 new int[100],后面也还是同一个问题
    ======================
    这样的话,你可以Clone
    int[] b=(int[])a.clone();
    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • yhef
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-24 10:32:2923楼 得分:0
    Java code
    public class Test { public static void main(String[] args) { int[] a = new int[] { 1, 2, 3, 4, 5 }; int[] b = new int[a.length]; new Test().copy(a, b); for (int i = 0; i < 5; i++) { System.out.println(b[i]); System.out.println(a[i]); } } public void copy(int[] a, int[] b) { System.arraycopy(a, 0, b, 0, a.length); } }



    修改 删除 举报 引用 回复
    进入用户个人空间
    加为好友
    发送私信
    在线聊天
    • fuqiang229
    • 等级:
    • 可用分等级:
    • 总技术分:
    • 总技术分排名:
    发表于:2008-08-24 22:12:0424楼 得分:0
    引用 22 楼 shengli_liao 的回复:
    // a变量中的值的个数不能确定,所以b没办法new一个int[],
    // 就是你new 了一个够大的 new int[100],后面也还是同一个问题
    ======================
    这样的话,你可以Clone
    int[] b=(int[])a.clone();

    你觉得这样可以??
    修改