public static void main ( String [] args ) {????????int [] numbers = { 1 , 2 , 3 , 4 , 5 , 6 };????????//数组的长度: 数组名 .length 其中 '.' 读作 的????????for ( int i = 0 ; i < numbers . length ; i ++ ){????????//访问数组中的元素: 数组名 [ 元素下标 ]????????System . out . println ( numbers [ i ]);????????}}
public static void main ( String [] args ) {????????int [] numbers = { 10 , 12 , 17 , 32 , 39 , 50 };????????for ( int i = 0 ; i < numbers . length ; i ++ ){????????????????if ( numbers [ i ] % 3 == 0 ){ // 被 3 整除????????????????????????// int result = numbers[i] * numbers[i];????????????????????????// numbers[i] = result;????????????????????????// numbers[i] = numbers[i] * numbers[i];????????????????????????numbers [ i ] *= numbers [ i ];????????????????}????????}}
public static void main ( String [] args ) {????????// A B C D E????????// A B F C D E????????String [] personArr = { "A" , "B" , "C" , "D" , "E" };????????String [] newArr = new String [ personArr . length + 1 ]; // 新建一个数组,长度比原来的数组多1????????int index = 2 ; //F 的位置????????for ( int i = 0 ; i < index ; i ++ ){????????????????newArr [ i ] = personArr [ i ]; // 将 personArr 数组中的元素直接拷贝过来????????}????????newArr [ index ] = "F" ;????????for ( int i = index ; i < personArr . length ; i ++ ){????????????????newArr [ i + 1 ] = personArr [ i ]; // 将 personArr 数组中 B 后面的元素挪动过来,但位置需要加1????????}????????personArr = newArr ;????????for ( int i = 0 ; i < personArr . length ; i ++ ){????????????????System . out . println ( personArr [ i ]);????????}}
public static void main ( String [] args ) {????????String [] personArr = { "A" , "B" , "F" , "C" , "D" , "E" };????????//A B F C D E????????//A B F D E????????String [] newArr = new String [ personArr . length - 1 ];????????int index = 3 ; //C 的位置????????for ( int i = 0 ; i < index ; i ++ ){????????????????newArr [ i ] = personArr [ i ];????????}????????for ( int i = index + 1 ; i < personArr . length ; i ++ ){????????????????newArr [ i - 1 ] = personArr [ i ];????????}????????personArr = newArr ;????????for ( int i = 0 ; i < personArr . length ; i ++ ){????????????????System . out . println ( personArr [ i ]);????????}}
System . arrayCopy ( 原数组 , 拷贝的开始位置 , 目标数组 , 存放的开始位置 , 拷贝的元素个数 );
public static void main ( String [] args ) {????????// A B C D E????????// A B F C D E????????String [] personArr = { "A" , "B" , "C" , "D" , "E" };????????String [] newArr = new String [ personArr . length + 1 ]; // 新建一个数组,长度比原来的数组多1????????int index = 2 ; //F 的位置????????// for(int i=0; i<index; i++){????????????????// newArr[i] = personArr[i];//将personArr 数组中的元素直接拷贝过来????????// }????????//数组拷贝????????System . arraycopy ( personArr , 0 , newArr , 0 , index );????????newArr [ index ] = "F" ;????????// for(int i=index; i<personArr.length; i++){????????????????// newArr[i+1] = personArr[i];//将personArr 数组中 B 后面的元素挪动过来,但位置需要加1????????// }????????System . arraycopy ( personArr , index , newArr , index + 1 ,????????personArr . length - index );????????personArr = newArr ;????????for ( int i = 0 ; i < personArr . length ; i ++ ){????????????????System . out . println ( personArr [ i ]);????????}}
数据类型 [] 标识符 = Arrays . copyOf ( 原数组 , 新数组的长度 );
public static void main ( String [] args ) {String [] personArr = { "A" , "B" , "C" , "D" , "E" };// 数组扩容: 第一个参数表示要扩容的数组 第二个参数表示,扩容后的新的数组的长度// 作用: 新建一个数组,并将原数组的所有元素全部拷贝至新数组中//newArr = {"A","B","C","D","E", null}String [] newArr = Arrays . copyOf ( personArr , personArr . length + 1 );int index = 2 ;// A B C D E null => A B C C D ESystem . arraycopy ( newArr , index , newArr , index + 1 , personArr . length -index );//A B C C D E => A B F C D EnewArr [ index ] = "F" ;personArr = newArr ;for ( int i = 0 ; i < personArr . length ; i ++ ){System . out . println ( personArr [ i ]);}}