1. What is an array
A array is a fixed length container that can hold data of the same type. We can operate the array through the index of the array, such as: get array data, modify data, delete data, etc. Array is a data type. To be exact, array is a reference type. For example, int is a basic type, while int [] which defines array is a reference type.
2. The definition and initialization of array
2.1 definition of array
The definition of array is very simple. The format is type [] array, such as int [] arr1, which means to define an integer array named arr1. Because an array is a reference type, defining an array is only to define a reference variable (that is, a pointer, although the concept of pointer is removed in Java). If the array is initialized, the reference variable will point to a block of heap memory.
2.2. Static initialization of array
During initialization, we explicitly specify the value of array elements, and the system determines the array length.
/** * @author RuiMing Lin * @date 2020-03-18 13:35 */ public class Demo1 { public static void main(String[] args) { // 1. Define an array named colors String[] colors; // 2. Initialize the array explicitly colors = new String[]{"yellow", "red" , "green" , "orange"}; // 3. Length of output array System.out.println(color.length); } }
Or you can simplify the initialization:
// Simplified static initialization String[] colors_1 = new String[]{"yellow", "red" , "green" , "orange"}; String[] colors_2 = {"yellow", "red" , "green" , "orange"};
2.3 dynamic initialization
We only specify the length of the array, and the system specifies the initial value for each array element:
// Define an array of strings with a length of 10 String[] colors = new String[10];
3. Operation of array
3.1 index and length of array
after we initialize the array, the computer will open up a continuous space in the disk according to the length of the array and the type of data stored. Each data will have its own number in order. From 0 to the array length minus one, this number is the index of the array.
An array has a built-in property: length, which can be used to get the length of the array.
/** * @author RuiMing Lin * @date 2020-03-18 13:35 */ public class Demo1 { public static void main(String[] args) { // Define an array int[] num = new int[]{0,1,2,3,4,5,6,7,8,9}; // Get data by index System.out.println(num[2]); // Modify data according to index num[2] = 10; System.out.println(num[2]); // Length of output array System.out.println(num.length); } }
3.2 traversal of array
There are many forms of array traversal. Here are two common ways:
If we are using IDEA to write code, we can use shortcut keys for array operations!
/** * @author RuiMing Lin * @date 2020-03-18 13:35 */ public class Demo1 { public static void main(String[] args) { // Define an array int[] num = new int[]{0,1,2,3,4,5,6,7,8,9}; // Traversal method 1: IDEA shortcut key: num.fori for (int i = 0; i < num.length; i++) { System.out.println(num[i]); } // Traversal method 2: IDEA shortcut key: num.for for (int i : num) { System.out.println(i); } } }
3.3. Get the maximum value of array
/** * @author RuiMing Lin * @date 2020-03-18 13:35 */ public class Demo1 { public static void main(String[] args) { // Define an array int[] num = new int[]{0,10,2,30,4,56,6,17,8,29}; int max = num[0]; //Traverse the array and take out each element for (int i = 0; i < num.length; i++) { if (num[i] > max) { max = num[i]; } } System.out.println("The maximum value of the array is: " + max); } }
3.4 reverse array
/** * @author RuiMing Lin * @date 2020-03-18 13:35 */ public class Demo1 { public static void main(String[] args) { // Define an array int[] num = new int[]{0,10,2,30,4,56,6,17,8,29}; for (int min = 0, max = num.length - 1; min <= max; min++, max--) { //Using temporary variables to exchange elements in arrays int temp = num[min]; num[min] = num[max]; num[max] = temp; } // After reversing, traverse the array for (int i = 0; i < num.length; i++) { System.out.println(num[i]); } } }
4. Array tool class Arrays
The java.utils package provides an array operation class to simplify array operation. Common classes include copying arrays, comparing arrays, sorting arrays, converting arrays into strings, etc.
import java.util.Arrays; /** * @author RuiMing Lin * @date 2020-03-18 13:35 */ public class Demo1 { public static void main(String[] args) { // Define an array int[] nums = new int[]{0,10,2,30,4,56,6,17,8,29}; // Sort arrays Arrays.sort(nums); // Convert array to string for output System.out.println("nums = " + Arrays.toString(nums)); // Replicated array int[] newNums = Arrays.copyOf(nums, nums.length); System.out.println(nums == newNums);// If two arrays are the same, the answer is false } }
5. Memory model of array
5.1 one dimensional array
When we define an array (not yet initialized), the system opens a space in the memory stack area to store the reference variable. When we initialize the array, the system opens a space in the heap memory for storage.
5.2 two dimensional array
Strictly speaking, Java does not have two-dimensional arrays, just looks like two-dimensional arrays. The essence of two-dimensional array is just that now stack memory defines a reference variable, pointing to another group of reference variables in heap memory, while the reference variables in heap memory point to stored data.
5.3, summary
when we define a variable, we only define a variable name in the stack memory before we assign a value to it or the system automatically assigns a value to it. At this time, the function of variable name is just like a pointer. When assigning a value to it, the system will open a piece of space in the heap memory to store data, and the variable will point to that piece of data area.