Array of Java learning notes

Posted by tarado on Thu, 11 Nov 2021 23:36:30 +0100

0x00 overview

This article mainly involves the Array of Java knowledge points.

 

0x01 array definition

An array is a container with a fixed storage length, which stores multiple data, and the data types should be consistent.

 

0x02 array format

2.1 type I

Data type [] array name

Example:

int[] arr;
double[] arr;
char[] arr;

  

2.2 second

Data type array name []

Example:

int arr[];
double arr[];
char arr[];

  

0x03 array dynamic initialization

3.1 what is dynamic initialization

Array dynamic initialization is to give only the length of the array and the system gives the default initial value.

 

3.2 dynamic initialization format

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

 

3.3 detailed explanation of dynamic initialization format

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
  • 5: Represents the length of the array

 

0x04 access to array elements

4.1 what is an index

Each element stored in the array will automatically have a number, starting from 0.

This automatic number is called the array index, and the elements in the array can be accessed through the index of the array.

 

4.2 access array element format

Array name[Indexes];

 

4.3 example code

package com.itheima;

public class ArrayDemo {
    public static void main(String[] args) {
        int[] arr = new int[3];

        // Output array name
        System.out.println(arr);

        //Output elements in array
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr[2]);
    }
}

 

0x05 memory allocation

5.1 memory overview

Memory is an important component of computer, temporary storage area, which 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 clear.

In order to run programs, Java virtual machine must allocate and manage memory space.

 

5.2 memory allocation in Java

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

 

 

 

0x06 memory map of single array

 

0x07 memory map of multiple arrays

 

 

  

0x08 multiple arrays point to the same memory map

 

 

 

 

 

  

0x09 array static initialization

9.1 what is static initialization

When creating an array, the elements are determined directly.

 

9.2 static initialization format

  • Full format
data type[] Array name = new data type[] {Element 1, element 2,....};

 

  • Simplified format
data type[] Array name = {Element 1, element 2,....}

 

9.3 example code

package com.itheima;

public class ArrayDemo2 {
    public static void main(String[] args) {
        // Define array
        int[] arr = {1, 2, 3};

        // Output array name
        System.out.println(arr);

        // Output elements in array
        System.out.println(arr[0]);
        System.out.println(arr[1]);
        System.out.println(arr[2]);
    }
}

  

Two common problems of 0x0A array operation

10.1 index out of bounds

  • Cause of occurrence
public class ArrayDemo {
    public static void main(String[] args) {
        int[] arr = new int[3];
        System.out.println(arr[3]);
    }
}

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 development, the out of bounds exception of the array cannot occur, otherwise the code will not continue to run.

  • Solution:

Modify the wrong index to be within the correct index range.

 

10.2 abnormal control

  • Causes:
public class ArrayDemo {
    public static void main(String[] args) {
        int[] arr = new int[3];

        // hold null Assign to array
        arr = null;
        System.out.println(arr[0]);          
    }
} 

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.

This problem should also be avoided in development.

  • Solution:

Just give the array a real reference to memory space

 

0x0B array convenience

Array convenience: it is to get each element in the array separately, that is, traversal.

Traversal is also the cornerstone of array operations.

public class ArrayTest01 {
    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, this writing method will certainly not work.

Therefore, we need to change the writing method into a loop. The index of the array is 0 to length-1, which can appear as a condition of the loop.

public class ArraryTest01 {
    public static void main(String[] args) {
        // 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]); 
        }
    }
}

  

0x0C array value

Get maximum value: find the maximum value from all elements of the array

Implementation idea:

  • Define variables to save the elements on the index of array 0;
  • Traversing the array to get each element in the array;
  • Compare the traversed element with the variable that holds the value on the index of array 0;
  • If the value of the array element is greater than the value of the variable, the variable is updated to the maximum value under the current comparison;
  • After the array loop is traversed, the variable saves the maximum value in the array;
package com.itheima;

public class ArrayTest2 {
    public static void main(String[] args) {
        // Define array
        int[] arr = {12, 45, 98, 73, 60, 5};

        // Define a variable to hold the maximum value
        // Remove the first data in the array as the initial value of the variable
        int max = arr[0];

        // Compare with the remaining data in the array one by one, and save the maximum value to the variable each time max in
        for (int x = 1; x < arr.length; x++) {
            if (arr[x] > max) {
                max = arr[x];
            }
        }

        // Print the value of the variable at the end of the cycle
        System.out.println("max: " + max);
    }
}