存放一维数组的数组
int[][] a = new int[2][3];
a[0].length;
a.length;
数组的长度一旦定义了,就无法修改。所以需要arraycopy()来重新分配一个新的 数组(扩容),然后将其内容拷贝进去。
int[] a = new int[5];
int[] b = new int[5];
a=b与System.arraycopy(a,0,b,0,5)有什么区别?
/*
* @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.
*/
public static native void arraycopy(Object src, int srcPos,Object dest, int destPos,int length);
...
参数的个数可变两个int相加?
public int add(int a,int b){}
三个int相加?
public int add(int a,int b,int c){}
四个int相加?
public int add(int a,int b,int c,int d){}
五个int相加?
public int add(int a,int b,int c,int d,int e){}
..........
上面的方案代码非常冗余。类似的代码重复写。
jdk1.5之前的解决方案:通过数组解决的
public int add(int[] a){
}
add(new int[]{5,8,9});
add(new int[]{5,8});
缺点:每次都要创建新的数组,
创建数组需要在堆区开辟连续的内存空间。
参数调用之后该变量就没用了,这样会
浪费内存空间。
jdk1.5之后的解决方案:通过...解决的
public int add(int... a){
}
调用: add(1,4);
add(1,4,6);
add(7,6,8,89,76);
注意: 1)可变长参数的使用和数组的使用是一样。 2)一个方法中只能定义一个可变长参数,并且要定义最后一个参数。