测试数组工具类Arrays

package arrays;

import java.util.Arrays;

//测试数组工具类Arrays
public class Test1_Arrays {

public static void main(String[] args) {
	method();//toString数组
	method2();//sore(数组)--把数组里的数据排序
	mothod3();//copyOf(m,n)--m是原数组的名称,n是新数组的长度
}
//copyOf(m,n)--m是原数组的名称,n是新数组的长度

private static void mothod3() {
	//1.定义原数组
	int[] c={5,8,9,7,3};
	//2.完成复制
	int[] d=Arrays.copyOf(c, 10);//复制数组必须有个新数组,数组一旦确定,长度不会改变,改变的只是新数组的长度,原来的数组长度不变
	//3.打印数据
	System.out.println(Arrays.toString(d));//[5, 8, 9, 7, 3, 0, 0, 0, 0, 0]把原来的数据复制完,在后面新增位置--扩容
	
	int[] i = {8,9,10};
	int[] j=Arrays.copyOf(i, 2);
	System.out.println(Arrays.toString(j));//新数组若是比原数组小,则会舍弃多出的部分--缩容
	
}

//sore(数组)--把数组里的数据排序
 private static void method2() {
	int[] b = {9,8,2,7,10,63,89};
	Arrays.sort(b);//数组排序不需要改变数组的长度,只会改变数据的位置,没有必要提供返回值 
	System.out.println(Arrays.toString(b));
	
}




private static void method() {
	//1.创建数组
	int[] a=new int[6];
	//2.遍历数组,并赋值
	/*for(int i=0;i<a.length;i++) {
		System.out.println(a[i]);
	}*/
      //3.打印数据
	Arrays.toString( a);
	System.out.println(Arrays.toString(a));
}

}

猜你喜欢

转载自blog.csdn.net/qq_47386653/article/details/107870262