Arrays.copyOf()用来实现数组的复制,返回一个数组,第一个参数是被复制的数组,第二个是复制的长度;
import java.util.Arrays;
public class Arraydemo{
public static void main(String[] args){
//数组的初始化;
String[] students=new String[]{"liming","jack","bob"};
String[] teachers={"happy","will","good"};
//遍历数组
System.out.println("--------students----------------");
for(int i=0;i<students.length;i++){
System.out.println(students[i]);
}
System.out.println("-----------teachers-------------");
for(int i=0;i<teachers.length;i++){
System.out.println(teachers[i]);
}
//数组的拷贝
System.out.println("----------person--------------");
String[] person=students;
for(int i=0;i<person.length;i++){
System.out.println(person[i]);
}
System.out.println("----------copyperson--------------");
String[] bigPerson=Arrays.copyOf(students,2*students.length);
for(int i=0;i<bigPerson.length;i++){
System.out.println(bigPerson[i]);
}
}
}
运行结果:
——–students—————-
liming
jack
bob
———–teachers————-
happy
will
good
———-person————–
liming
jack
bob
———-copyperson————–
liming
jack
bob
null
null
null