preface
This paper is mainly from the container concept to the concept of array and then to the three definitions of array and various cases of array memory analysis, as well as some commonly used array traversal and inversion, maximization and other operations. Designed to provide a comprehensive understanding of Java arrays. @[toc]
1. What is a container
Before we get to arrays, it’s important to talk about the concept of containers where you store multiple pieces of data together, and each piece of data is called an element of that container.
Containers in our lives: water cups, wardrobes, classrooms
2. What is an array
An array is a container that stores data of a fixed length, ensuring that multiple data types are consistent.
3. Three definitions of arrays
Here are three ways to define an array.
Definition Method 1
Data type [] Array name = new data type [length];
Example: Define an array container that can store three integers, such as int[] arr = new int[3]
Definition Method 2
Data type [] array name = new data type []{element 1, element 2, element 3… };
Example: define an array container that stores integers 1,2,3,4,5. Int [] arr = new int[]{1,2,3,4,5};
Definition Method 3
Data type [] Array name = {element 1, element 2, element 3... };
Int [] arr = {1,2,3,4,5};
Array definition format:
Data types stored in arrays: What data types can be stored in the array container created. [] : indicates an array. Array name: Specifies a variable name for the array that meets the identifier specification. You can use the name to manipulate the array new: keyword, which is used to create the array. Data types stored in arrays: What data types can be stored in the array container created. [Length] : The length of the array, indicating how many elements can be stored in the array container. Note: Arrays have a fixed length. Once specified, the length cannot be changed. Same with water cup, buy a 2 liter water cup, the total capacity is 2 liters, no more, no less.
4. Array access
Index: Starting at 0, the index accesses the elements of the array.
Format: Array name [index]
Array length property: The length of the array is fixed. Since the index starts at 0, the maximum index value of the array is the name of the array. Length-1
public static void main(String[] args) {
int[] arr = new int[]{1.2.3.4.5};
// Prints the properties of the array. The output is 5
System.out.println(arr.length);
}
Copy the code
5. What is memory
Before we talk about arrays and memory, we have to understand something called memory.
Memory is an important part of a computer, the temporary storage area used to run programs. The program we write is stored in the hard disk, the program in the hard disk is not run, must be put into memory to run, after running will empty the memory. To run programs, Java virtual machines must allocate and manage memory space.
5.1 Allocating Java VM Memory
In order to improve the efficiency of computing, the space is divided into different areas, because each area has a specific way of processing data and memory management.
5.2 Array storage in memory
5.2.1 An array memory map
public static void main(String[] args) {
int[] arr = new int[3];
System.out.println(arr);//[I@5f150435
}
Copy the code
The value of the new variable is stored in heap memory, and the value of the arr variable is stored in the address of the array.
If you print arr[0], you will print the element on index 0 of the array where arR is stored. If there is no specific value, it is the default value of the type. Such as:
String[] arr=new String[3];
System.out.println(arr); // [Ljava.lang.String;@1b6d3586
System.out.println(arr[0]); // null
int[] arrInt=new int[3];
System.out.println(arrInt); // [I@1b6d3586
System.out.println(arrInt[0]); / / 0
Copy the code
5.2.2 Memory map of two arrays
public static void main(String[] args) {
int[] arr = new int[3];
int[] arr2 = new int[2];
System.out.println(arr);
System.out.println(arr2);
}
Copy the code
5.2.3 Two variables pointing to an array
public static void main(String[] args) {
// Define an array to store 3 elements
int[] arr = new int[3];
// The array index is assigned
arr[0] = 5;
arr[1] = 6;
arr[2] = 7;
// Outputs the element values on the three indexes
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
// Define the array variable arr2 and assign the address of arR to arR2
int[] arr2 = arr;
arr2[1] = 9;
System.out.println(arr[1]);
}
Copy the code
6, array common exceptions
Array common exceptions are array out-of-bounds exceptions and null pointer exceptions, this is very basic, not to do more introduction, here mainly analyzes the null pointer exceptions in memory
7. Array traversal
Array traversal is to get each element of the array separately, traversal. It is very important to go through the groups!!
public static void main(String[] args) {
int[] arr = { 1.2.3.4.5 };
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
System.out.println(arr[3]);
System.out.println(arr[4]);
}
Copy the code
The above code can iterate through every element in the array, but if the array has a lot of elements, I’m going to call this fool-proof, which is not going to work, so we need to change to a loop. The index of the array is 0 to lenght-1, which can be used as a condition of the loop. The following
public static void main(String[] args) {
int[] arr = { 1.2.3.4.5 };
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i])
}
}
Copy the code
8. Array retrieves the maximum element
Implementation approach: define variables, save the elements of the array index of 0 Through the array, for each element of the array will traverse to the array elements and 0 index value of the variable is used in the comparison If the value is greater than the value of a variable of array elements, variable record live end of the array to iterate over the new values, variables is the maximum value of the array
Be careful not to be fascinated by the first and second woman
public static void main(String[] args) {
int[] arr = { 5.15.2000.10000.100.4000 };
// Define a variable to hold the element with index 0 in the array
int max = arr[0];
// Walk through the array and fetch each element
for (int i = 0; i < arr.length; i++) {
// Compare the iterated elements to the variable Max
// If the array element is larger than Max
if (arr[i] > max) {
// Max records large values
max = arr[i];
}
}
System.out.println("Array maximum is:" + max);
}
Copy the code
9. Array inversion
An inversion is a reversal of the array elements
Ideas: the most distant element of the array switch places Realize the reversal, you need to the far end of the array position exchange of two variables, save the array index of minimum and maximum index elements of the two indexes swap places Minimum index + +, maximum index -, swap places again Minimum index exceeds the maximum index, array reverse operation over
The specific code is as follows
public static void main(String[] args) {
int[] arr = { 1.2.3.4.5 };
/* Define variable min=0 min index Max =arr.length‐1 Max index min++, Max ‐ */
for (int min = 0, Max = arr. Length ‐1; min <= max; Min++, Max ‐ ‐) {// Use a third-party variable to swap elements in the array
int temp = arr[min];
arr[min] = arr[max];
arr[max] = temp;
}
// After the inversion, iterate over the number group
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i])
}
}
Copy the code
Arrays as method parameters and return values
Let’s be clear: the array returns the memory address of the array
public static void main(String[] args) {
// Call the method to receive the return value of the array
// The memory address of the array is received
int[] arr = getArray();
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i])
}
}
Return Returns the address of the array */
public static int[] getArray() {
int[] arr = { 1.3.5.7.9 };
// Return the address of the array to the caller
return arr;
}
Copy the code
11. Method parameter type difference
Methods whose arguments are primitive are passed data values. When the argument to a method is a reference type, the address value is passed.
Analyze the following program code and calculate the output.
public static void main(String[] args) {
int a = 1; int b = 2;
System.out.println(a);
System.out.println(b);
change(a, b);
System.out.println(a);
System.out.println(b);
}
public static void change(int a, int b) {
a = a + b; b = b + a;
}
Copy the code
- Analyze the following program code and calculate the output.
public static void main(String[] args) {
int[] arr = {1.3.5};
System.out.println(arr[0]);
change(arr);
System.out.println(arr[0]);
}
public static void change(int[] arr) {
arr[0] = 200;
}
Copy the code
Conclusion: Methods whose arguments are primitive types are passed data values. When the argument to a method is a reference type, the address value is passed.
I believe that you have copied the above code into Eclipse or IDEA to run, is the result surprising? And don’t quite understand the following summary? Boy boots, I suspect you may have missed this point: Array is a reference type, the element type of the array itself does not affect, just like the array elements are int, array is still a reference type, ha ha, small white students really lovely ~ shot, shot, click a thumbup
Welcome to pay attention to my public number, discuss technology together, yearning for technology, the pursuit of technology…