import java.util.Arrays;
import java.util.List;
public class Example4_1 {
public static void main(String[] args) {
int[] num = new int[10]; //动态初始化数组
int[] b = new int[20];
for(int i = 0;i<10;i++){ //给数组赋值
num[i] = i;
System.out.print(num[i] +" ");
}
//数组元素拷贝
//方法原型:public static native void arraycopy(Object src,int srcPos,Object dest,int destPos,int length);
//作用:从src数组的srcPos位置往dest数组的destPos位置拷贝length个元素
System.arraycopy(num,0,b,0,4);
for(int i = 0;i<10;i++){
System.out.print(b[i] +" ");
}
//Arrays.equals:判断数组内容是否相等
System.out.println(Arrays.equals(num,b));
String[] bp = new String[10];
Arrays.fill(bp,"b"); //Arrays.fill:数组填充内容
//方法原型:public static void fill (Object[] a,Object val);
System.out.println(Arrays.toString(bp));
//Arrays.List:将数组转换为List
List<String> asList = Arrays.asList(bp);
System.out.println(bp);
int[] a = {2,3,5,6,8,7,4};
Arrays.sort(a);//数组排序
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(num));
}
}
运行结果如下:
0 1 2 3 4 5 6 7 8 9 0 1 2 3 0 0 0 0 0 0 false
[b, b, b, b, b, b, b, b, b, b]
[Ljava.lang.String;@1b6d3586
[2, 3, 4, 5, 6, 7, 8]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]