1. Array [Dark Horse JavaSE Notes]

Article directory


(1) Format of array definition (recommended to use format 1)

Format 1: data type [ ] variable name


         int[] arr

An array of int type is defined, and the array name is arr

Format 2: data type variable name [ ]


     	int  arr[]

A variable of type int is defined, and the variable name is an array of arr

(2) Initialization of the array

1. Overview of array initialization

Arrays in java must be initialized before they can be used

The so-called initialization: It is to allocate memory space for the array elements and assign a value to each array element.

2. Array initialization method

1) Dynamic initialization

[Only specify the length of the array during initialization, and the system assigns the initial value to the array]

Format: array type [] variable name = new data type [array length];

new: apply for memory space for the array


		int[] arr = new int[3]
2) Static initialization

[Specify the initial value of each array element during initialization, and the length of the array is determined by the system]

Format: data type [ ] variable name = new data type [ ] {data1, data2, data3, ...};


int[] arr = new int[]{1,2,3};

Simplified format: data type [] variable name = {data1, data2, data3, ...};


int[] arr = {1,2,3};

Program example:

public static void main(String[] args) {
    int[] arr = {1,2,3};
    System.out.println(arr); // [I@119d7047
    System.out.println(arr[0]); // 1
    System.out.println(arr[1]); // 2
    System.out.println(arr[2]); // 3

(3) Array element access

1. Array variable access method

Format: array name

2. How to access the data stored inside the array

Format: arrayname[index]

[The index is the numbering method of the data in the array]

Function: The index is used to access the data in the array. The array name [index] is equivalent to the variable name, which is a special variable name

Feature ①: Index starts from 0

Feature ②: The index is continuous

Feature ③: The index increases one by one, adding 1 each time

Program example:

public static void main(String[] args){
        int[] arr = new int[3];
        //output array name
        System.out.println(arr);// [I@119d7047 This is the address value of the memory space requested by the array
        //elements in the output array
        System.out.println(arr[0]);// 0
        System.out.println(arr[1]);// 0 
        System.out.println(arr[2]);// 0

(4) Memory allocation in Java

0. The default value of the data type

When the array is initialized, a default value is added to the storage space

  • integer: default value 0
  • Float: default value 0.0
  • boolean: default false
  • char: the default is the null char
  • Reference data type: the default value is null [null: empty value, the default value of the reference data type, indicating that it does not point to any valid object]

1. Stack memory

[Storing local variables]

Disappear immediately after use

2. Heap memory

[Storing new content (entities, objects)]

Every new thing has an address value

After use, it will be reclaimed when the garbage collector is idle.

3. Multiple arrays point to the same memory map

Program example:

public static void main(String[] args){
    int[] arr1 = new int[3];
    arr1[1] = 200;
    arr1[2] = 300;
    System.out.println(arr1); // [I@119d7047
    System.out.println(arr1[1]);// 200
    System.out.println(arr1[2]);// 300

    int[] arr2 = arr1;  // When multiple arrays point to the same memory map, when the value of one array changes, the value of the other array will also change with it
    arr2[1] = 220;
    arr2[2] = 330;
    System.out.println(arr1); // [I@119d7047
    System.out.println(arr2); // [I@119d7047
    System.out.println(arr1[1]);// 220
    System.out.println(arr1[2]);// 330
    System.out.println(arr2[1]);// 220
    System.out.println(arr2[2]);// 330

(5) Two common minor problems in array operations

1. Index out of bounds

[The element corresponding to the index that does not exist in the array is accessed, causing the index out of bounds problem]

int[] arr = new int[3];
//Return elements that do not exist in the array
System.out.println(arr[3]); // ArrayIndexOutOfBoundsException (index out of bounds)

2. Null pointer exception

[The accessed array no longer points to the data in the heap memory, causing a null pointer exception]

int[] arr = new int[3];
//assign null to the array
arr = null;
//output element
System.out.println(arr[2]); // NullPointerException (null pointer exception)

(6) Common operations on arrays

1. traverse

General format:

int[] arr = {1, 2, 3, 4};
// arr.length is used to get the length of the array
for (int i = 0; i < arr.length; i++) {

2. Get the maximum value in the array

General format:

int[] arr = {100, 99, 55, 66, 88, 11};
// Define a variable to store the maximum value, and store the first element of the array in this variable
int max = arr[0];
// Compare the size of this maximum value with every other element of the array, and reassign the larger value to this variable for each comparison
for (int i = 1; i < arr.length; i++) {
    if (arr[i] > max) {
        max = arr[i];
// output maximum
System.out.println("max:" + max);

Tags: Java

Posted by Ancoats on Wed, 23 Nov 2022 15:27:58 +0530