Java entry notes-array (two)

table of Contents

Introduction

Summary of Array Introduction 

Array definition format

Introduction to array initialization

Dynamic initialization

Assignment and use of array elements

Static initialization

Two-dimensional array

Dynamic initialization

Static initialization

Memory allocation in the array

Array common exception

Array index out of bounds

Null pointer exception

Common operations in arrays

Traverse the array

Get the maximum 

 


Introduction

If the program requires 5 variables of type int, using the basic knowledge we have learned, we can use the following code to declare variables:

int a1,a2,a3,a4,a5;

But suddenly one day when we need to declare a hundred variables, what should we do? You can't copy a hundred times, right?

In order to solve this solution, we need to learn the knowledge of arrays.

 

An array is a compound data type composed of variables of the same type (an array is a collection of variables of the same type), and these variables of the same type are called elements or units of the array.

Summary of Array Introduction 

 

  1. Declare a large number of variables at once
  2. The same type of data to be stored

 

Array definition format

There are two formats for declaring one-dimensional arrays

 

Format 1:

//数组类型 [] 数组名
int [] arr;
//定义一个数组,数组名为arr

 

Format 2:

//数组类型 数组名[] 
char arr[];
//定义一个char类型的变量,变量名为arr的数组

 

You can declare multiple arrays at once:

int [] a,b,c,d,e;

 

Introduction to array initialization

Arrays in Java must be initialized before they can be used 

The so-called initialization: is to allocate memory space for the array elements in the array, and assign values ​​to each array element

 

Dynamic initialization

Dynamic initialization: only specify the length of the array during initialization, and the system will assign an initialization value to the array

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

 

demonstration:

int [] arr = new int[3];

Analysis:

  • int []: indicates that this is an array of type int
  • arr: array name
  • new: apply for memory space for the array
  • int [3]: It means that this is an array of type int, and 3 is the length of the array, which is the number of elements stored in the array

 

Assignment and use of array elements

 

Before assignment, you need to understand the index. The index is the numbering method of the data in the array.

  • Function: Index is used to access the data in the array, the array name [index] is equivalent to the variable name, it is a special variable name
  • Feature 1: Index starts from 0
  • Feature 2: Index is continuous
  • Feature 3: Indexes increase one by one, each time increasing by 1

 

 

We try to access the elements in the array. Before assigning a value, the data in the array are all 0.

 

 

Array assignment

Format: array name [index] = constant

demonstration:

arr[0] = 1;
arr[1] = 2;
arr[2] = 3;

 

 

Static initialization

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 [] {data 1, data 2, data 3,...};
  • Example: int[] arr = new int[] {1,2,3};

 

  • Simplified format: data type [] variable name = {data 1, data 2, data 3,...};
  • Example: int[] arr ={1,2,3};

 

 

Multiple static array initialization:

int arr[] = {1,2,3},arr1 [] ={5,6,7};

 

Two-dimensional array

A two-dimensional array is composed of several one-dimensional arrays, and a two-dimensional array can be vividly represented by a table in Excel.

Like this table, a two-dimensional array can be represented (the figure shows a two-dimensional array with 3 rows and 2 columns)

 

Declare a two-dimensional array:

  • Array type array name [] [];
  • Array type [] [] array name; 

 

Dynamic initialization

After the two-dimensional array is declared, the new operation allocates space for the array:

int arr[][] = new int[3][2];

The two-dimensional array arr created by the appeal can represent 3 rows and 2 columns 

 

Demonstration of two-dimensional array assignment:

arr[0][0] = 1;
arr[0][1] = 1;

 

Static initialization

Static initialization can allocate elements of different lengths for each one-bit array

Format: data type two-dimensional array name [] = { {first row element}, {second row element}}

demonstration:

int arr[][] = {
        {1},
        {2,3,4},
        {5,6}
};
System.out.println(arr[0][0]);
System.out.println(arr[2][1]);

 

Memory allocation in the array

When a java program is running, it needs to allocate space in the memory. In order to improve computing efficiency, the space is divided into different areas, because each area has a specific data processing method and memory management method.

After we allocate the memory unit for the array, we can see the address of the array in the memory unit when we print the array without index.

It should be noted that for char type arrays, when System.out.println() prints the name of the array, it does not print the address of the memory unit, but the value of all the elements in the array.

When you want to print the address of the memory unit of the char type array, you need to concatenate the array and the string.

char a[] = {'h','e','l','l','o',' ','w','o','r','l','d'};
System.out.println(""+a);

 

 

Taking the int array as an example, when initializing, a default value is added to the storage space:

 

  • Integer: default value 0
  • Floating point number: the default value is 0.0
  • Boolean: the default value is false
  • Character: Default value null character
  • Reference data type: the default value is null

 

note

  • The variables stored in the stack memory are local variables and will disappear after use 
  • The heap memory will store the default value added by the space, and every new thing has an address value
  • After use, it will be recycled by the garbage collector

 

When we assign values ​​to the elements in the array, the heap memory will change

After an array has created the memory space, assign one array to another array (the two array types must be the same).

Multiple arrays will point to the same address.

So, when you modify the elements in the array arr2, you will find that the elements in the array arr have also changed 

 

Memory allocation in two-dimensional array

 

Array common exception

  • Index out of bounds: access to the element corresponding to the index that does not exist in the array, according to the index out of bounds problem
  • Null pointer exception: The accessed array no longer points to the data in the heap memory, and it becomes a null pointer exception

 

Array index out of bounds

ArrayIndexOutOfBoundsException

You only created 3 spatial positions, but you want to get the variable of the fourth space.

 

 

Null pointer exception

NullPointerException

The array has no memory unit address in the stack memory. If we still get the element from the memory unit, a null pointer error will be reported

 

 

Common operations in arrays

Traverse the array

length is used to get the number of elements in the array

In the for loop, use arr.length to make conditional judgments

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

for (int i = 0 ;i<arr.length;i++){
    System.out.println(arr[i]);

 

Get the maximum 

int arr[] = {11,2,333,44};
        int max = arr[0];

        for (int i = 0 ;i<arr.length;i++){
            if(arr[i] > max){
                max = arr[i];
            }
        }
        System.out.println(max);

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_41924764/article/details/110262245