Java basic knowledge points
One, the array
1. Array assignment method:
1.String[] arr=new String[]; //[] write the length of the array in brackets
2.String[] arr2=new String[]{“a1”,”a2”,”a3”};
3.String[] arr3={“a1”,”a2”,”a3”,”a4”};
2. The definition of the array:
String[] arr;
The development of the array is in the stack space, and the addresses of the values in the array are stored in the heap space
arr.length=The length of the array.
Traverse the array-enhanced for-original version:
for(int i=0;i<arr.length;i++){ int a=arr[i]; sout(a); }
Traverse the array-enhanced for-simplified version:
for(int a:arr){ sout(a); }
3. The sorting methods in the array are:
Bubble Sort
Select sort
Quick sort
4. Common methods of the Arrays class:
equals(); Compare whether the values of the elements in the two arrays are exactly the same, and return true/false.
Usage: array name 1.equals (array name 2)
sort(); Sort the array in ascending order.
Usage: Arrays.sort (array name);
to String(); Convert an array to a string type and protect it with [].
Usage: Arrays.to String (array name)
fill(); Replace all elements in the array with a new value.
Usage: Arrays.fill (array name, element value);
copyOf(); Copy a copy
Usage: int[] newArr=Arrays.copyOf(array name, new array length);
binarySearch(); Find the subscript corresponding to the element, provided that the array is already in ascending order.
Usage: int i=Arrays.binarySearch (array name, element to be searched);
5. Array search method (premise: the array is already in ascending order):
Binary search method (recursive and loop two methods)
Two, two-dimensional array:
1. The definition of a two-dimensional array:
//Declaration-take the name
int[][] arr, arr2;
//Specify length-allocate space
arr=new int[3][];
//Integrate the above two steps-the length of the two-dimensional array must be specified, and the length of the one-dimensional array may not be specified first
int[][] arr3=new int[3][5];
2. The method of storing data in a two-dimensional array:
1. One element to
int[][] arr3=new int[1][2];
arr3[0][0]=1;
arr3[0][1]=2;
2. A one-dimensional array to
int[][] arr2=new int[3][];
arr2[0]=new int[2];
arr2[1]=new int[]{1,2,3} ;
2.
Int[][] arr3=new int[][]{
{1},{1,2},{1,2,3}};
int[][] arr4={
{1} {2}{1,2}};
3. Take out the value:
int[][] arr={
{1},{1,2},{1,2,3}};
//Traverse a two-dimensional array
for(int i=0;i<arr.length;i++){ / / Traverse one-dimensional array for(int j=0;j<arr[i].length;j++){ sout(arr[i][j]); } }