java foundation - array
Definition of array
- An array is an ordered collection of the same data type
- Array describes several data of the same type, which are arranged and combined in a certain order
- Among them, each data is called an array element, and each array element can access them through a subscript, starting from 0.
expression:
int [] arr=new int [10]//It can store 10 numbers of type int //Assignment. If there is no assignment, the default value is 0 arr[0]=1; arr[1]=2;
Get array length
//Variable name Length to get the length of the array int [] arr= new int[10]; System.out.println(arr.length);
Memory analysis
As shown in the figure:
We declare an array of integer type, which is 10 in length
In the stack, the variable (array) of the reference object is placed, because the length is 10 Therefore, a space is opened in the heap, and 10 small spaces are opened in the space to store the values of the array
Array initialization
An entity class can also be an array
//Man is an entity class Man [] mans={new Man(), new Man()};|
Basic characteristics
- Its length is determined. Once an array is created, its size cannot be changed.
- Its elements are of the same type, and mixed types are not allowed
- The elements in the array can be any data type, including basic data type and reference type
- Array variables are reference types. Arrays can also be regarded as objects. Each element in the array is equivalent to the member variables of the object. The array itself is an object, and the Java object is in the heap. Therefore, whether the array saves the original type or other object types, the array object itself is in the heap
Array boundary
Legal range of subscript: [0,length-1]. If it exceeds, an error will be reported;
int [] arr=new int[2]; System.out.println(arr[2]);
ArrayIndexOutOfBoundsException: array subscript out of bounds exception!
Use of arrays
Basic usage:
Enhanced for loop:
/*The output is no different from the ordinary for loop, but there is no subscript, There is no way to manipulate the values in the array*/ int [] arr= {1,2,3,4,5,6}; for(int arrs : arr){ System.out.println(arrs); }
Small exercise:
Invert the values in the array
Multidimensional array
- Multidimensional arrays can be regarded as arrays. For example, a two-dimensional array is a special one-dimensional array, and each element is a one-dimensional array.
- 2D array:
//It can be regarded as an array of two rows and five columns int [][] a= new int[2][5]; /*int[2]It can be seen as creating two spaces in the heap int[5]Look, the two spaces in the pile have five small spaces*/ int [][] b={{1,2,3,4,5},{1,2,3,4,5}}; //It is the same as the two-dimensional array a above
Loop through two-dimensional array:
Arrays class
- Array tool class java util. Arrays
- There are no methods for us to call array objects, but the API provides a tool class Arrays for us to use, so we can perform some basic operations on data objects
- The methods in the Arrays class are static methods decorated with static. When using them, you can call them directly with the class name instead of using the object (Note: it is "no" instead of "no")
function
-
Assign a value to the array: through the fill method
Note:
Arrays.fill( a9, value );
a9 is an array variable, and value is a value of the element data type in a9. Function: each element in the a1 array is filled with value
Arrays.fill(a9, 3, 5,"World");
The first parameter refers to the array of operations. The second and third parameters refer to inserting the fourth parameter into an area of the array. The second parameter refers to the starting element subscript (including the subscript), and the third parameter refers to the ending subscript (excluding the subscript). Note: the array subscript of java starts from 0 -
Sort the array: sort in ascending order through the sort method
-
Compare arrays: use the equals method to compare whether the element values in the array are equal
About the difference between = = and equals:
For details, please refer to the boss blog -
Find array elements: binary search can be performed on the sorted array through the binarySearch method
Printing arrays without looping
One dimensional array:
int [] a ={1,2,3}; System.out.println(Arrays.toString(a));
2D array:
[[ I@15db9742, [I@6d06d69c ]This output result is due to:
Because arrs is a two-dimensional array. It is equivalent to an array of length 2, but the elements of this array are arrays.
When executing arrays ToString is equivalent to traversing the array and outputting the elements of the array, but the elements of the array are arrays, so the address of the array elements is output here.
Sparse array
- It is also a data structure, also known as compression algorithm
- When most elements in an array are 0 or an array with the same value, you can use a sparse array to save the array.
Treatment method:
- How many rows and columns are there in the record array? How many different values are there
- The elements, rows, columns and values with different values are recorded in a small-scale array, so as to reduce the size of the program
As shown in the following figure: the original array is on the left and the sparse array is on the right
Don't say much, code!
Step 1: let's define the same array on the left:
Step 2: view the total number of valid values of the original array, define a sparse array, and design the header
Step 3: traverse the original array and store the non-zero value of the original array in the sparse array
Step 4: the sparse array has been completed. Print the output
Full code:
// Let's first define an array of the left graph, 6 rows and 7 columns int[][] arr = new int[6][7]; // Let the value of the array be the same as the left figure arr[0][3] = 22; arr[0][6] = 15; arr[1][1] = 11; arr[1][5] = 17; arr[2][3] = -6; arr[3][5] = 39; arr[4][0] = 91; arr[5][2] = 28; // Traversal array for (int i = 0; i < arr.length; i++) { for (int j = 0; j <= arr.length; j++) { System.out.print(arr[i][j] + "\t"); } System.out.println(); } // View valid values int sum = 0;// Number of valid values for (int i = 0; i < arr.length; i++) { for (int j = 0; j <= arr.length; j++) { if (arr[i][j] != 0) { sum++; } } } System.out.println(sum); // Define a sparse array, as shown in the right figure. We can see that its columns are fixed, and the row is the total number of valid values + 1, because the first row is the total number of row and column added values of the original array int[][] arrs = new int[sum + 1][3]; // Design head arrs[0][0] = arr.length;// The first row and the second column are the rows of the original array arrs[0][1] = arr[0].length;// The first row and the second column are the columns of the original array arrs[0][2] = sum;// The first row and the third column are the total number of valid values of the original array // Print the sparse array and look at the header System.out.println("================================"); for (int i = 0; i < arrs.length; i++) { for (int j = 0; j < arrs[0].length; j++) { System.out.print(arrs[i][j] + "\t"); } System.out.println(); } // Store the non-zero value of the original array into a sparse array int count = 0;// The valid value defined is 0 for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[0].length; j++) { if (arr[i][j] != 0) { count++;// Add 1 as long as there is a non-zero value // arrs[count]: rows that record non-zero as a sparse array arrs[count][0] = i;// The first column of each row is i: that is, the row of the original array arrs[count][1] = j;// The second column of each row is j: that is, the column of the original array arrs[count][2] = arr[i][j];// The third column of each row is the value: that is, the non-zero value of the original array } } } System.out.println("================================"); // Traversal output sparse array for (int i = 0; i < arrs.length; i++) { for (int j = 0; j < arrs[0].length; j++) { System.out.print(arrs[i][j] + "\t"); } System.out.println(); }
Restore sparse array
code:
/*Restore the sparse array and define a new array First row of sparse array arrs: 6 seven eight arrs[0][0]Is 6, that is, the number of rows of the original array arr arrs[0][1]Is 7, that is, the number of columns of the original array arr*/ int [][] NewArr = new int [arrs[0][0]][arrs[0][1]]; //Loop through the sparse array arrs and give the value to the restored array NewArr for(int i=1;i<arrs.length;i++){//i=1: the first line is the row, column and value of the original array arr, which does not need to be traversed /*The nonzero values of the original array arr are sorted in the sparse array arrs: 6 7 8 0 3 22 0 6 15 When I is equal to 1: arrs[i][0]=0, [arrs[i][1]=7; arrs[i][2]=22; So newarr [ARRS [i] [0] [ARRS [i] [1]] = ARRS [i] [2] = newarr [0] [3] = 22; The sparse array is restored */ NewArr[arrs[i][0]][arrs[i][1]]=arrs[i][2]; } //Print restore array NewArr System.out.println("Print restore array NewArr:"); for (int i = 0; i < NewArr.length; i++) { for (int j = 0; j < NewArr[0].length; j++) { System.out.print(NewArr[i][j]+"\t"); } System.out.println(); }