Article Directory
Array definition:
1. Declare variables
int[ ] a;
2. Create an array
int[ ] a=new int[100];
3. Array assignment
a)int[ ] smallPrimes={2,3,4,5,7,11,13};`
b)可以用for循环赋值
4. Anonymous functions
new int [ ]{1,2,3,4};
可以用此方法初始化一个数组,无需创建新变量
int smallPrimes=new int[]{1,2,3,4};
这是下列语句的简写形式:
int[] anonymous={1,2,3,4};
smallPrimes=anonymous;
Print array
1. for loop traversal
2. for each loop
for (variable: collection) statement
for example:
for(int element:collection)
System.out.println(element);
System.out.println(Arrays.toString(collection));
Array copy
1. Assign an array variable to another array variable, and the two variables point to the same array.
int[] smallPrimes={2,3,4,5,7,11,13};`
int [] a=smallPrimes;
a[5]=12;
2. If you want to copy all the values of an array to a new one To go to an array, use the copyOf method of the Arrays class:
int[] b=Arrays.copyOf(a,a.length);
The second parameter is the length of the new array. This method is usually used to increase the size of the array:
int[] c=Arrays.copyOf(a,2*a.length);
Array sort
Arrays.sort(a);
Reference: Java Core Volume 1 finishing