java array definition, usage, and array memory analysis

Preface This paper is mainly from the concept of container to the concept of array to the three definitions of array and the analysis of array memory ...
1. What is a container
2. What is array
3. Three definitions of array
4. Access to array
5. What is memory
6. Common array exceptions
7. Array traversal [key]
8. Get the maximum value element from the array
9. Array inversion
10. Array as method parameter and return value
11. Parameter type difference of method

Preface

This paper is mainly from the concept of container to the concept of array to the three definitions of array and the analysis of array memory in various situations, as well as some common operations of array traversal, inversion, maximum value and so on. It aims to have a comprehensive understanding of java arrays. @[toc]

1. What is a container

Before talking about arrays, it is necessary to talk about the concept of containers. Containers are the storage of multiple data together, and each data is called the element of the container.

Containers in life: water glass, wardrobe, classroom

2. What is array

The so-called array is the < font color = Red > container with a fixed length of < font color = Red > to store data, ensuring that the data types of < font color = Red > of multiple data are consistent.

3. Three definitions of array

Here are three ways to define arrays.

Definition method 1

Data type [] array name = new data type [length];

For example: define an array container that can store 3 integers, such as: int[] arr = new int[3]

Definition mode 2

Data type [] array name = new data type [] ;

For example: define an array container to store 1, 2, 3, 4, 5 integers int[] arr = new int[];

Definition mode 3

Data type [] array name = ;

For example: define the array container int[] arr = for storing 1,2,3,4,5 integers;

Array definition format details:

Data type of array storage: what data type can be stored in the created array container. []: represents an array. Array name: set a variable name for the defined array to meet the identifier specification. You can use the name to operate the array new: keyword, the keyword used to create the array. Data type of array storage: what data type can be stored in the created array container. [length]: the length of an array, indicating how many elements can be stored in the array container. Note: array has fixed length property. Once the length is specified, it cannot be changed It's the same with the water cup. If you buy a 2-liter water cup, the total capacity is 2 liters, which can't be more or less.

4. Access to array

Index: starting from 0, an index can access elements in an array.

Format: array name [index]

Array length attribute: the array length is fixed. Since the index starts from 0, the maximum index value of the array is array name. length-1

public static void main(String[] args) { int[] arr = new int[]; //Print the properties of the array. The output is 5 System.out.println(arr.length); }

5. What is memory

Before we talk about the principle of array memory, we must understand the concept of memory.

Memory is an important component of a computer, a temporary storage area, which is used to run programs. The program we wrote is stored in the hard disk. The program in the hard disk will not run. It must be put into the memory to run. After running, the memory will be emptied To run programs, Java virtual machine must allocate and manage memory space.

5.1 memory partition of Java virtual machine

In order to improve the efficiency of operation, the space is divided into different regions, because each region has a specific way of data processing and memory management.

5.2 storage of array in memory 5.2.1 an array memory diagram
public static void main(String[] args) { int[] arr = new int[3]; System.out.println(arr);//[I@5f150435 }

The output of the above method is [I@5f150435. What is this? Is the address of the array in memory. The contents of new are stored in the heap memory, while the variable arr in the method stores the address of the array.

Output arr[0], the elements on the 0 index in the array in the memory address saved by arr will be output. If there is no specific value, it is the default value of the type! For example:

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

5.2.2 memory diagram 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); }

5.2.3 two variables point to an array
public static void main(String[] args) { // Define array, store 3 elements int[] arr = new int[3]; //Array index for assignment arr[0] = 5; arr[1] = 6; arr[2] = 7; //Output element values on 3 indexes System.out.println(arr[0]); System.out.println(arr[1]); System.out.println(arr[2]); //Define the array variable arr2, assign the address of arr to arr2 int[] arr2 = arr; arr2[1] = 9; System.out.println(arr[1]); }

6. Common array exceptions

The common exceptions of array mainly include the array out of bounds exception and null pointer exception, which is very basic, so we will not introduce them. Here we mainly analyze the situation of null pointer exception in memory

7. Array traversal [key]

The so-called array traversal is to get each element of the array separately, that is, traversal. Traversal array is very important!!!

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]); }

The above code can traverse all the elements in the array, but if there are many elements in the array, I call this method fool traversal, which is definitely not good, so we need to transform it into circular writing. The index of the array is 0 to light-1, which can be used as a condition of the loop. as follows

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]) } }

8. Get the maximum value element from the array

Implementation ideas: Define variables to hold elements on the index of array 0 Traverse the array to get every element in the array Compare the traversed element with the variable holding the value on the index of array 0 If the value of an array element is greater than the value of the variable, the variable records the new value When the array loop is over, the variable holds the maximum value in the array

< font color = Red > true fragrance warning < / font >: please be careful not to be fascinated by the first and second women The code is as follows:

public static void main(String[] args) { int[] arr = { 5, 15, 2000, 10000, 100, 4000 }; //Define variables to hold the elements of the 0 index in the array int max = arr[0]; //Traverse the array and take out each element for (int i = 0; i < arr.length; i++) { //max comparison of traversed elements and variables //If the array element is greater than max if (arr[i] > max) { //max record the maximum value max = arr[i]; } } System.out.println("The maximum value of the array is: " + max); }

9. Array inversion

The so-called inversion is to reverse the position of array elements

Implementation idea: elements at the far end of an array are interchanged To achieve inversion, you need to exchange the positions of the farthest elements of the array Define two variables to hold the minimum and maximum indexes of the array Exchange position of elements on two indexes minimum index + +, maximum index --, exchange position again Minimum index exceeds maximum index, array inversion operation ended

The specific code is as follows

public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; /*min=0 minimum index defined in loop max=arr.length‐1 Maximum index min++,max‐‐ */ for (int min = 0, max = arr.length ‐ 1; min <= max; min++, max‐‐){ //Using the third-party variable to complete the exchange of elements in the array int temp = arr[min]; arr[min] = arr[max]; arr[max] = temp; } // After reversing, traverse the array for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]) } }

10. Array as method parameter and return value

First, make it clear: as the return value of the method, the array returns the memory address of the array

public static void main(String[] args) { //Call method to receive the return value of the array //Received memory address of array int[] arr = getArray(); for (int i = 0; i < arr.length; i++) { System.out.println(arr[i]) } } /* Create method, return value is array type create method, return value is array type return Returns the address of the array */ public static int[] getArray() { int[] arr = { 1, 3, 5, 7, 9 }; //Returns the address of the array to the caller return arr; }

11. Parameter type difference of method

When the parameter of method is of basic type, data value is passed. When the parameter of method is of reference type, 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; }
  1. Analyze the following program code and calculate the output.
public static void main(String[] args) { int[] arr = ; System.out.println(arr[0]); change(arr); System.out.println(arr[0]); } public static void change(int[] arr) { arr[0] = 200; }

Summary: when the method parameter is of basic type, data value is passed. When the method parameter is of reference type, address value is passed

I believe that you have copied the above code to eclipse or idea. Is the result unexpected? What's more, I don't understand the following summary very well? I think you may have overlooked this point: array is a reference type, and the element type of array itself has no influence, just like array elements are all ints, array is still a reference type, ha ha, Xiaobai is lovely. If you get shot, please praise it. Support it. Pay attention to the building owner. The building owner will teach you java, and pay attention to the building owner~

You are welcome to pay attention to my public address, explore technology, yearn for technology and pursue technology.

4 November 2019, 19:53 | Views: 9905

Add new comment

For adding a comment, please log in
or create account

0 comments