Java entry (4) -- array, number of java Entry Articles
In the previous article, we introduced an array concept in foreach. arrays are the most common data structures. They are basic data sequences or object sequences encapsulated together with an identifier of the same type.
An array is a set of data with the same data type. It can be divided into one-dimensional array, two-dimensional array, and multi-dimensional array based on different dimensions. You can regard one dimension as a straight line, and two dimensions as a plane and three-dimensional as a three-dimensional space.
1. One-dimensional array 1. Create one-dimensional array
Array as an object allows memory allocation using the new keyword. Before using an array, you must first define the type of the array variable, that is, declare an array.
The Declaration array has two forms, which are also mentioned in the previous chapter. The syntax format is as follows:
Array Element type array name []; Data Element type [] array name;
Programmers are used to the second method when writing code. Note that no data can be added. The following is an example of declaring arrays. Both methods are correct. Different types of arrays must be declared for different data:
Int arr []; // declares an int array. Each element in the array is an int value String [] str; // declares a String array. Each element in the array is a String array.
After declaring an array, you cannot access any of its elements. To use an array, you must allocate memory space for it and specify the length of the array when allocating memory space. The syntax format is as follows:
Array name = new array element type [number of array elements];
The following example shows how to allocate a memory space of 5 to the array:
arr = new int[5];
The storage status of the one-dimensional array arr is as follows:
Arr [0] |
Arr [1] |
Arr [2] |
Arr [3] |
Arr [4] |
0, 1, 2, 3, and 4 in brackets indicate the subscript of the array. Note that the subscript starts from 0 and ends with an array length of-1.
Of course, you can also directly declare and allocate the memory as follows:
int[] week = new int[7];
The code above creates a one-dimensional array week and specifies the array length to 7.
Note that when the new keyword is used to allocate memory to the array, the initialization value of each element in the array is 0. For example, if the preceding Code uses the new Keyword to create a week array with a length of 7, the elements in the array can be expressed as [0, 0, 0, 0, 0, 0]. this is a one-dimensional array. every element in the array is initialized to 0.
2. initialize a one-dimensional array
The initialization mentioned above is to use the new keyword for automatic initialization. The array can also be initialized by the programmer like the basic data type, and each element in the array can be initialized separately.
There are two methods for Array initialization:
Int arr1 [] = new int [] {1, 2, 3, 4, 5, 6}; // The first int arr2 [] = {10, 11, 12, 13, 14}; // The second
The array initialization method is to include data in braces and separate the values of elements in the array with commas in the middle. The system automatically allocates a certain amount of space for the array. The first method creates an array of six elements, whose values are 1, 2, 3, 4, 5, and 6 in sequence. The second method creates an array of five elements, the values are 10, 11, 12, 13, and 14 in sequence.
The following is an example of a one-dimensional array based on the Process Control described in the previous article.
Eg: Calculate the sum of each element in a one-dimensional array
1 public class SumNum {2 3 public static void main (String [] args) {4 int [] num = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // create and initialize the array 5 int sum = 0; // store the accumulate and 6 7 for (int I = 0; I <num. length; I ++) {8 if (I = num. length-1) {// determine whether the subscript is the last 9 System. out. print (num [I] + "="); // output equal sign 10} else {11 System. out. print (num [I] + "+"); // output the plus sign 12} 13 sum + = num [I]; 14} 15 16 System. out. print (sum); 17} 18 19}
Here, num is the array name, and the total length of this array is 10. But in general programming, when you need to modify the length of the array, assume that I <10 is written in the for loop, therefore, unexpected errors may occur when we modify the code. Therefore, we should habitually use the array name when writing the code. length to obtain the length of the array, such as num in the above Code. length is actually 10. Num. length-1 is because the subscript of the array starts from 0. When we get the last element, the subscript of the array is the total length minus one, so num. length-1 indicates that the subscript of the last element 10 is 9. The final running result is:
2. Multi-dimensional array 1. Two-dimensional array
If each element in a one-dimensional array is still a one-dimensional array, It is a two-dimensional array. A two-dimensional array is usually used to represent a table. Information in the table is organized in rows and columns. The first subscript represents the row where the element is located, and the second subscript represents the column where the code element is located. Or in terms of linear algebra, a two-dimensional array is a matrix. The first subscript represents the row of the matrix, and the second subscript represents the column of the Matrix.
Two-dimensional array declaration methods are also available. The syntax format is as follows:
Array Element type array name [] []; array element type [] [] array name;
Like a one-dimensional array, if no memory space is allocated during declaration, the keyword new is also used to allocate memory.
A two-dimensional array can be viewed as composed of multiple one-dimensional arrays. When allocating memory to a two-dimensional array, the same memory can be allocated to these one-dimensional arrays at the same time. The array in the first square brackets is the number of one-dimensional arrays, and the second square brackets are the length of one-dimensional arrays.
int arr = new int[2][4];
The above code is a typical declaration of a two-dimensional array arr and allocate memory space for it. After the allocation, arr has two one-dimensional arrays with a length of 4. The memory allocation is as follows:
Arr [0] [0] |
Arr [0] [1] |
Arr [0] [2] |
Arr [0] [3] |
Arr [1] [0] |
Arr [1] [1] |
Arr [1] [2] |
Arr [1] [3] |
We can also allocate memory for each dimension separately. As follows:
int a = new int[];a[0] = new int[2];a[1] = new int[3];
In this way, we can obtain a two-dimensional array. The length of the first one-dimensional array is 2, and the length of the second one-dimensional array is 3.
The initialization of a two-dimensional array is similar to that of a one-dimensional array. You can also use braces to initialize a two-dimensional array. As follows:
int arr[][] = {{3,0},{5,7},{6,9}};
This is an array of arr [3] [2]. It has three one-dimensional arrays, and the length of each one-dimensional array is 2. However, it should be clear that the subscript starts from. For example, arr [1] [0] = 5 indicates that the first element of the second one-dimensional array is 5. Therefore, you can assign a value to arr [x] [y] directly. For example, assign a value to the second element of arr [1:
arr[1][1] = 50;
The preceding array is in the following format:
int arr[][] = {{3,0},{5,50},{6,9}};
2. 3D Array
For three-dimensional arrays, we can estimate that we can use one square brackets for one dimension and two square brackets for two dimensions. Then, the three-dimensional array uses three brackets.
int arr[][][] = new int[][][]{ {{1,2,3},{4,5,6}}, {{7,8,9},{10,11,12}}, {{13,14,15},{16,17,18}} };
3. Basic operations on arrays 1. Traverse Arrays
Traversing an array is to obtain each element in an array. Usually, traversing an array is implemented through a for loop.
To traverse a one-dimensional array, you only need to use a for loop, as shown in the following example:
int[] week = {1,2,3,4,5,6,7}for(int i=0; i<7; i++) { System.out.println(week[i]);}
Another way to traverse is to use the foreach statement, which has been mentioned at the end of the previous article. Here we will not repeat it too much and go directly to the Code:
for(int arr : week) { System.out.println(arr);}
Traversing two-dimensional arrays is a little more troublesome than one-dimensional arrays. You need to use a double-layer loop, as shown in the following example:
Int a [] [] = {1, 2}, {3, 4, 5}, {6, 7, 8, 9 }}; for (int I = 0; I <. length; I ++) {for (int j = 0; j <a [I]. length; j ++) {System. out. print (a [I] [j] + ""); // output} System. out. println (); // line feed}
Note that the first for loop traverses rows, so the length is. length, while the second for loop traverses the column. The length is a [I]. length.
Emphasize the difference between print and println again. print is output but not wrap, and println is output and wrap, so the output result is as follows:
2. Sort Arrays
Arrays can be sorted by using the Arrays class in the java. util package. The sort () method provides many reloads to sort Arrays of any type in ascending order. The syntax format is Arrays. sort (object );
Eg: Create an array, sort it, and output it.
1 public class Taxis {2 3 public static void main (String [] args) {4 int [] arr = new int [] {17, 21, 6, 59, 31, 13, 3}; 5 6 System. out. println ("original array:"); 7 for (int I = 0; I <arr. length; I ++) {8 System. out. print (arr [I] + ""); 9} 10 11 Arrays. sort (arr); // sort by dictionary 12 13 System. out. println ("\ n sorted array:"); 14 for (int I = 0; I <arr. length; I ++) {15 System. out. print (arr [I] + ""); 16} 17} 18 19}
In Java, the String-type array Sorting Algorithm sorts data in alphabetical order. Therefore, numbers are placed before letters and uppercase letters are placed before lowercase letters. The running result is as follows:
3. Other operations
There are also many operations on Arrays, such as filling the fill method for replacing array elements, copying the copyOf Method for Arrays, and so on. The Arrays class also provides other methods for operating Arrays, you can learn how to use them by reading materials or using APIs.
Another important knowledge point is the sorting algorithm, which already has multiple loops, which can lead to many sorting algorithms, such as Bubble sorting and selective sorting, if I have time in the future, I may summarize various sorting algorithms, such as bubble, selection, direct insertion, fast, merge, Hill, and heap sorting, this is one of the essential knowledge points for learning programming languages. There is also a lot of information on the Internet.