//一个小小的关于传值的问题,以前没留意过这个问题,还挺有意思的.
//这里有个一小程序,作用就是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);

}}

解决方案 »

  1.   

    没人回答呀,分少了吗?呵呵.我看了一下 java 的API,System.arraycopy(a,b);和我的问题很像
      

  2.   

    恕小弟才疏学浅看不懂 我就想这个System.arraycopy()是怎么实现的呢?
      

  3.   

    /**
         * 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);
      

  4.   

    从指定源数组中复制一个数组,复制从指定的位置开始,到目标数组的指定位置结束。从 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 - 要复制的数组元素的数量。 
      

  5.   


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

  6.   

    native方法应该没有开源的代码,如果想知道就问sun去
    int[] a = new int[]{1,2,3,4,5}; 
    int[] b = new int[a.length]; 
    System.arraycopy(a, 0, b, 0, a.length); 
    方法只能改变Object参数的属性,不能改变参数本身
      

  7.   

    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);  } 
    }
      

  8.   

    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;

    }  
      

  9.   

    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;
    }
    }
      

  10.   

    你也看的出来,char数组中有多少数据是在方法中在能知道的.
      

  11.   

    不知道用hascode行不行呵呵..........可惜我不会.
      

  12.   

    怎么可能完成啊.....
    两个不同的引用变量.如果你在外边创建好数组对象,然后传进来还差不多吧.
    外边的是null,你的形参传也是null,然后你形参创建了一个对象,实参并没有改变啊.也就是程序外边的b还是null;
      

  13.   


    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的定义拿出来,不然就成了在函数内新建一个数组了
      

  14.   

    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;
    } }
      

  15.   

    把copy方法改成int[]的啊,然后return不就得了么
      

  16.   

    // a变量中的值的个数不能确定,所以b没办法new一个int[], 
    // 就是你new 了一个够大的 new int[100],后面也还是同一个问题 
    ======================
    这样的话,你可以Clone
    int[] b=(int[])a.clone();
      

  17.   

    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);
    }
    }
      

  18.   

    你在方法中让b = new int[]{0,0,0,0,0};当然不行。因为你让b指向了一个新的对象了。它和实参指向的对象已经不一样了。
    可以考虑在方法调用之前让b初始化为一个比较大的数组(能满足a的大小要求),这样把a中的值依次拷贝到b中去。
    或者提供一个辅助类
    class ArrayHelper{
        public int[] b;
    }
    将这个类的对象作为参数,改写方法,通过改变对象的内容实现你的需求。
      

  19.   

    引用 22 楼 shengli_liao 的回复:
    // a变量中的值的个数不能确定,所以b没办法new一个int[], 
    // 就是你new 了一个够大的 new int[100],后面也还是同一个问题 
    ====================== 
    这样的话,你可以Clone 
    int[] b=(int[])a.clone(); 
     
    你觉得这样可以??
    ==================
    你觉得不可以?
    什么都要试试
    问别人是问不出结果的
      

  20.   

    方法里不能让b=new int....
      

  21.   

    只能提供一个封装类来实现了。
    毕竟java只提供pass by value这种传值模型。
    如果能提供像C++一样的传引用就好了。