Define one-dimensional array, traversing, copying, expansion and volume reduction
1. The one-dimensional array: is a collection of the same data type, a contiguous memory
array definition :
definition of the way a: int []arr=new int[]{1,2,3};
the definition of two ways: int []arr=new int[5];
// Default: 0
define three ways:int []arr={1,2,3}
2.new: open up the memory on the heap
3.arr.length: the length of the array arr
4. array traversal :
(1)
for(int index=0;index<arr.length;index++)
System.out.println(arr[index]);
(2)
for(int value:arr) System.out.println(value);
(3)
System.out.println(Arrays.toString(arr));
5.ArrayIndexOutOfBoundsExcception: cross-border abnormal array subscript
6. The array copy
1)for(int i=0;i<arr.length;i++ ) brr[i]=arr[i];
2) int []brr=arr.clone();
3) brr= Arrays.copyOf(arr,arr.length);
4) System.arraycopy(arr,0,brr,0,arr.length);
7. shallow copy: There is a copy of the contents out of the shared memory // are generally shallow copy
dark Copy: copy the contents out of the shared memory does not exist, a different address
8. The method defined grow expansion
public static void grow(int []arr)
{
int brr[]=new int[arr.length]+1;
System.arraycopy(arr,0,brr,0,arr,length);
return brr;
}
arr=grow(arr);
9.System.arraycopy () higher than Arrays.copyOf operating efficiency, but Arrays.copyOf () is more convenient
Defined two-dimensional array, iterate, copy
An array of arrays - each element is a two-dimensional array of one-dimensional array
1. The two-dimensional array of definitions :
int[][] arr = new int[3][5]; //定义了一个整型的二维数组,其中包含3个一维数组,每个一维数组可以存储5个整数。
int[][] arr = new int[3][]; //表示一个包含了三个整型的一维数组的二维数组。
int[][] arr = {{2,5},{1},{3,2,4},{1,7,5,9}}; //注意:[]在变量名前的时候,是紧跟数据类型的;如果[]在后,则是属于当前变量名。
2. The two-dimensional array of copies of :
(1)
for(int i=0;i<arr.length;i++)
for(int j=0;j<arr[i].length;j++)
brr[i][j]=arr[i][j];
(2)
int [][]brr=Arrays.copyOf(arr,arr.length); //浅拷贝,brr使用的仍是同一块数据,地址一样。
for(int i=0;i<arr.length;i++)
brr[i]=Arrays.copyOf(arr[i],arr[i].length);
3. The two-dimensional array traversal :
int [][]b={{2,2},{3,2}};
for(int i=0;i<b.length;i++)
for(int j=0;j<b[0].length;j++)
System.out.print(b[i][j]+" ");
4.arr.length: a few lines, i.e., several one-dimensional array
arr [0] .length: there are several columns, i.e., each one-dimensional array Several
5, the string is converted to char array:
String str="abcde";
char[] c=new char[str.length()];
c=str.toCharArray();
6.next () with a space, carriage return, tab and other end of the string, nextline only a carriage return
7.split ( "*"): to split a string **
String []arr=str.split(" ");