array
Array overview
An array is a collection of a set of data with the same data type.
One dimensional array
Array declaration
Data type array name[]; int array[];//Defines a variable of type int, whose name is array. data type[] Array name; int[] array;//Defines an array of type int, and the array name is array.
Array initialization
Arrays in java must be initialized before they can be used.
Initialization: allocate memory space for the array elements in the array and assign values to each array element.
dynamic initialization
Only the array length is specified during initialization, and the system allocates the initial value for the array.
data type[] Variable name = new data type[Array length]; int[] array = new int[3];//When using the new keyword to allocate memory for an array, the initial value of each element in an integer array is 0. //Integer: default 0 //Floating point number: the default value is 0.0 //Boolean: the default value is false //Character: the default value is an empty string //Reference data type: the default value is null
initiate static
The initial value of each element is specified during initialization, and the length of the array is determined by the system.
data type[] Variable name = new data type[]{Data 1, data 2, data 3,...}; int[] array = new int[]{1,2,3}; int[] array2 = {1,2,3};//simplify
Use of arrays
//Output array name System.out.println(array);//[I@4554617c //Output array elements System.out.println(array[0]);//0 System.out.println(array[1]);//0 System.out.println(array[2]);//0
Traversal of one-dimensional array
int[] array = new int[]{1,2,3,4,5}; for(int i = 0 ; i<array.length ; i++){ System.out.println(array[i]); }
Memory allocation in Java
Common problems of array operation
- Subscript out of bounds: the element corresponding to the non-existent subscript in the array is accessed, resulting in the subscript out of bounds.
- Null pointer exception: the accessed array no longer points to the data in heap memory, resulting in null pointer exception.
Null: null value, which refers to the default value of the data type, indicating that it does not point to any valid object.
Get latest value
//Get latest value int[] array = {12,45,98,73,60}; //Define two variables and take the first data of the array as the initial value int max = array[0];//Save Max int min = array[0];//Save minimum //Compare with the remaining data in the array one by one for(int i=1 ; i<array.length ; i++){ if(array[i] > max){ max = array[i];//Save the maximum value to the variable max for each comparison }else if(array[i] < min){ min = array[i];//Save the minimum value to the variable min for each comparison } } //Print the value of the variable at the end of the cycle System.out.println("The maximum value is:" + max ); System.out.println("The minimum value is:" + min );
Two dimensional array
Two dimensional arrays can be regarded as special one-dimensional arrays.
Create a 2D array
int[][] array = new int[2][3];//A two-dimensional array array1 is created, including two one-dimensional arrays with a length of 3 int[][] array2 = new int[2][];//Created a two-dimensional array array2 with a length of 2 array2[0] = new int[3];//Allocate memory for each dimension separately array2[1] = new int[4];
2D array initialization
int[][] array3 = {{1,2,3},{4,5,6},{7,8,9}}; array3[1][1]=2;//Single assignment
Traversing a two-dimensional array
int[][] array3 = {{1,2,3},{4,5,6},{7,8,9}}; for(int i=0; i<array3.length ; i++){ for(int j=0; j<array3[i].length; j++){ System.out.print(array3[i][j] + " "); } System.out.println();//Output space }
Basic operation of array
Fill replace array elements
After the element definition of the array is completed, you can replace the elements in the array through the static method fill() of the Arrays class.
fill(int[ ] a , int value)
This method assigns the specified int value to each element of an int array.
int[] a = new int[5]; Arrays.fill(a,8); for(int i = 0 ; i<a.length ; i++){ System.out.print(a[i] + " "); } //Output result: 8
fill(int[ ] a , int fromIndex , int toIndex , int value)
This method assigns the specified int value to each element within the specified range of the int array. The range of padding is from index fromIndex (included) to index toIndex (not included).
If fromIndex == toIndex, the fill range is empty.
int[] a = new int[5]; Arrays.fill(a,1,3,8); for(int i = 0 ; i<a.length ; i++){ System.out.print(a[i] + " "); } //Output result: 0 8 0 0
Sort arrays
You can sort Arrays through the static sort() method of the Arrays class.
The sort() method provides a variety of overloaded forms, which can sort arrays of any type in ascending order.
Arrays.sort(object) / / where object refers to the name of the array to be sorted.
int[] array = new int[]{6,1,3,2,5,4}; Arrays.sort(array); for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } //Output result: 1 2 3 4 5 6
Copy array
The copyOf() method and copyOfRange() method of Arrays class can copy Arrays.
The copyOf() method copies the array to the specified length, and the copyOfRange() method copies the specified length of the specified array to a new array.
copyOf(arr , int newlength)
newlength refers to the length of the copied new array. If the length of the new array is greater than the length of array arr, it is filled with 0. (the filling content depends on the type of copied array)
int[] array = new int[]{1,2,3,4,5,6,7,8,9}; int[] newarray = Arrays.copyOf(array,5); for (int i = 0; i < newarray.length; i++) { System.out.print(newarray[i] + " "); } //Output result: 1 2 3 4 5
copyOfRange(arr , int fromIndex , int toIndex)
fromIndex: Specifies the index position at which to start copying the array. fromIndex must be between 0 and the length of the entire array. The new array includes elements whose index is fromIndex.
toIndex: the last index position of the range to be copied. Can be greater than the length of array arr. The new array does not include elements whose index is toIndex.
int[] array = new int[]{1,2,3,4,5,6,7,8,9}; int[] newarray1 = Arrays.copyOfRange(array,1,5); int[] newarray2 = Arrays.copyOfRange(array,1,11); for (int i = 0; i < newarray1.length; i++) { System.out.print(newarray1[i] + " "); } System.out.println(); for (int i = 0; i < newarray2.length; i++) { System.out.print(newarray2[i] + " "); } //Output result: 2 3 4 5 //Output result: 2 3 4 5 6 7 8 9 0
Array query
The binarySearch() method of the Array class can use the binary search method to search the specified Array to obtain the specified object.
This method returns the index value of the element to be searched.
binarySearch(Object[ ] a , Object key)
a: Array to search
key: value to search
If the key is included in the array, the index of the search value is returned; Otherwise, the range is - 1 or "-" (insertion point).
The insertion point is the point where the search key will be inserted into the array, that is, the first element index greater than this.
int[] array = new int[]{6,1,3,2,5,4}; Arrays.sort(array);//sort int index = Arrays.binarySearch(array,2); System.out.println(index); int index2 = Arrays.binarySearch(array,9); System.out.println(index2); //Output result: 1 //Output result: - 7
Note: the array must be sorted (via the sort() method) before this call is made. If the array is not sorted, the result is uncertain. If the array contains more than one element with the specified value, there is no guarantee which one is found.
int[] array = new int[]{6,4,3,2,5,4,1}; int index = Arrays.binarySearch(array,1); System.out.println(index); //Output result: - 1
binarySearch(Object[ ] a , int fromIndex , int toIndex , Object key)
This method retrieves an element within the specified scope.
fromIndex: Specifies the index (inclusive) at the beginning of the range.
toIndex: the index at the end of the specified range (excluding).
int[] array = new int[]{1,2,3,4,5,6,7,8,9}; int index = Arrays.binarySearch(array,1,2,3); System.out.println(index); //Output result: - 3
Before using this method, the array should also be sorted to obtain the accurate index value. If the element key to be searched is within the specified range