java数组的复制、扩充、缩容
作者:互联网
package cn.tedu.oop;
import java.util.Arrays;
public class TestArray {
public static void main(String[] args) {
//创建一个无序数组
int [] from={1,9,7,0,2};
//数组的普通复制
//第一个参数:复制哪个数组。第二个参数:数组的长度
int [] to= Arrays.copyOf(from,5);
System.out.println(Arrays.toString(to));
//3.数组的扩容
int [] to2 = Arrays.copyOf(from,10);
System.out.println(Arrays.toString(to2));
//4.数组的缩容
int [] to3=Arrays.copyOf(from,3);
System.out.println(Arrays.toString(to3));
//5.指定收尾元素截取数组中的个数
//该方法指定的是数组的下标,左闭右开,含头不含尾[2,4),所以打印7和0
int [] to4=Arrays.copyOfRange(from,2,4);
System.out.println(Arrays.toString(to4));
}
}
标签:缩容,java,Arrays,System,int,toString,数组,out 来源: https://blog.csdn.net/qq_52226783/article/details/117717048