Array and memory allocation

Posted by aswini_1978 on Tue, 28 Dec 2021 10:35:02 +0100

1. Array

An array is a container for storing data with a fixed length. The data types for storing multiple data should be consistent.

1.1 array definition format

data type[] Array name
int[] arr;
double[] arr;
char[] arr;

Data type array name[]
int arr[];
double arr[];
char arr[];

1.2 array initialization

Array dynamic initialization

Dynamic initialization: when creating an array, specify the length of the array, and the virtual machine sets the default value for the elements of the array

data type[] Array name = new data type[Array length];
int[] arr = new int[3];
int arr[] = new int[3];

To the left of the equal sign:
int:Data type of array
[]:Represents that this is an array
arr:Represents the name of the array
    
To the right of the equal sign:
new:Open up memory space for arrays
int:Data type of array
[]:Represents that this is an array
3:Represents the length of the array

Array static initialization

Static initialization: when creating an array, the element data in the array is directly specified, and the length is automatically calculated by the virtual machine

Full version format
 data type[] Array name = new data type[]{Element 1,Element 2,...};
int[] arr = new int[]{1,2,3};

Simplified format
 data type[] Array name = {Element 1,Element 2,...};
int[] arr = {1, 2, 3};

1.3 array element access

Indexes

Each element stored in the array will automatically have a number, starting from 0.
This automatic numbering is called array index, and the elements in the array can be accessed through the index of the array.

Access array element format

Array name[Indexes];

int[] arr = new int[3];
//Output array name
System.out.println(arr); //[I@880ec60
//Output elements in array
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);

1.4 memory allocation

Memory overview

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

Java virtual machine occupies local memory by default, with a minimum of 1 / 64 and a maximum of 1 / 4 of local memory

Memory allocation in java

At present, we only need to remember two memories: stack memory and heap memory

Area nameeffect
registerIt has nothing to do with our development.
Native Method Stack JVM is used when using operating system functions, which has nothing to do with our development.
Method areaStore class files that can be run.
Heap memoryStorage objects or arrays, created by new, are stored in heap memory.
Method stackThe memory used when the method runs, such as the main method, enters the method stack for execution.

The basic data type is stored in the stack, and the reference data type (new) is stored in the heap

Variables are stored in stack memory and objects are stored in heap memory

If the basic data type is directly stored in the variables in the stack, if it is a reference data type, the address value in the heap memory is stored in the variables in the stack

int[] arr = new int[5];
    variable        object

2. Array traversal

Is to get each element in the array separately, which is traversal. Traversal is also the cornerstone of array operations.

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, this writing method will certainly not work, so we
 It needs to be transformed into circular writing. The index of the array is 0 to lenght-1 ,Can appear as a condition of a loop.
//Define array
int[] arr = {11, 22, 33, 44, 55};
//Use a common traversal format
for(int x=0; x<arr.length; x++) {
System.out.println(arr[x]);
}

3. Two common problems of array operation

3.1 index out of bounds exception

Causes:
The array length is 3 and the index range is 0 ~ 2, but we accessed an index of 3.
After the program runs, an array out of bounds exception of ArrayIndexOutOfBoundsException will be thrown. In the development, the out of bounds exception of the array cannot occur. Once it occurs, we must modify the code we write.

Solution:
Modify the wrong index to the correct index range!

int[] arr = new int[3];
System.out.println(arr[3]);

3.2 null pointer exception

Causes:
The line of code arr = null means that the variable arr will not save the memory address of the array, so it is not allowed to operate the array. Therefore, a null pointerexception null pointer exception will be thrown during operation. In the development, the null pointer exception of the array cannot occur. Once it occurs, we must modify the code we write.

Solution:
Give the array a real heap memory space reference!

int[] arr = new int[3];
//Assign null to array
arr = null;
System.out.println(arr[0]);

4. Array type

All basic data types can be arrays. The address value appears in the array name directly, while the array content appears in the char array. In order to appear the address value, the array name needs to be printed toString()

Array dynamic initialization String The default value is null,char The default value is empty, boolean Default to false,Other is 0
String arr[] = new String[3];
arr[0] = "c";
arr[1] = "b";
System.out.println(arr);
System.out.println(arr[2]);//null

Topics: Java data structure