Chapter 6 array

Keywords: Java

1, Array definition and access

1.1 array concept

  • Array concept: an array is a container for storing data with a fixed length to ensure that the data types of multiple data are consistent.

1.2 definition of array

Mode 1
  • Format:

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

  • Detailed explanation of array definition format:

    • Data type of array storage: what data type can the created array container store.
    • []: represents an array.
    • Array name: name a variable for the defined array, which meets 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 the created array container store.
    • [length]: the length of the array, indicating how many elements can be stored in the array container.
    • Note: the array has the fixed length feature. Once the length is specified, it cannot be changed.
      • The same as the water cup, I bought a 2-liter 2-point water cup with a total capacity of 2 liters, neither more nor less.
  • give an example:
    Define an array container that can store 3 integers. The code is as follows:

int[] arr = new int[10]
Mode II
  • Format:
data type[] Array name = new data type[]{Element 1,Element 2,Element 3,...};
  • give an example:
    Defines an array container that stores 1, 2, 3, 4, 5 integers.
int[] arr = new int[]{1,2,3,4,5};
Mode III
  • Format:
Array type[] Array name = {Element 1,Element 2,Element 3,...};
  • give an example:
    Defines an array container that stores 1, 2, 3, 4, 5 integers
int[] arr = {1,2,3,4,5};

1.4 access to arrays

  • 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.
  • Format:
Array name[Indexes]
  • Length attribute of array:
    Each array has a length and is fixed. Java gives an attribute to the array to obtain the length of the array. The statement is: array name. Length, the execution result of attribute length is the length of the array, and the int type result. Since it can be inferred that the maximum index value of the array is the array name. length-1.
public static void maiin(String[] args){
	int[] arr =  new int[]{1,2,3,4,5};
	//Print the properties of the array, and the output result is 5
	System.out.println(arr.length);
}
  • Index the elements of the access array:
    • Array name [index] = numeric value, assigning values to the elements in the array
    • Variable = array name [index], get the elements in the array
public static void main(String[] args){
	//Define and store an array of int types, and assign elements 1, 2, 3, 4, 5
	int[] arr = {1,2,3,4,5};
	//Assign a value of 6 to the 0 index element
	arr[0]=6;
	//Gets the element on the index of array 0
	int i = arr[0];
	System.out.println(i);
	//Direct output array 0 index elements
	System.out.println(arr[0]);
}

2, Array schematic memory diagram

2.1 memory overview

Memory is the primary and temporary storage area in the computer, which is used to run programs. The program we write is stored in the hard disk. The program in the hard disk will not run. It must be put into the memory to run. Empty the memory after running.

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

2.2 memory partition of Java virtual machine

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

  • Memory partition of JVM:

2.3 storage of array in memory

An array memory graph
public static void main(String[] args){
	int[] arr = new int[3]
	System.out.println(arr);//[I@f150435
}

When the above method is executed, the output result is[ I@f150435 , what is this? It is the address of the array in memory. The contents of new are stored in heap memory, while the variable arr in the method stores the address of the array.

Two array memory graphs
public static void main(String[] args){
	int[] arr = new int[3];
	int[] arr2 = new int[2];
	System.out.println(arr);
	System.out.println(arr2);
}

Two variables point to an array
public static void main(String[] args){
	//Define an array to store 3 elements
	int[] arr = new int[3]
	//Array index 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 and assign the address of arr to arr2
	int[] arr2 = arr;
	arr2[1]=9;
	System.out.println(arr[1]);
}

3, Common operations for arrays

3.1 array out of bounds exception

Look at the code and see what happens when it runs.

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

Create an array and assign three elements. The index of the array is 0, 1 and 2. There is no 3 index, so we can't access the index that doesn't exist in the array. After the program runs, an ArrayIndexOutOfBoundsException array out of bounds exception will be thrown. In development, the out of bounds exception of the array can't occur. Once it occurs, we must modify the code we write.

3.2 array null pointer exception

Look at the code and see what happens when it runs.

public static void main(String[] args){
	int[] arr = {1,2,3};
	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. In development, the out of bounds exception of the array cannot occur. Once it occurs, we must modify the code we write.

Performance of null pointer exception in memory graph

3.3 array traversal [ key ]

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, so we need to transform it into a circular writing method. The index of the array is 0 to length-1, which can appear as a circular condition.

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

3.4 get maximum value element of array

  • Get maximum value: find the maximum value from all elements of the array.
  • Implementation idea:
    • Define variables to hold the elements on the index of array 0
    • Traverse 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 records the new value
    • After the array loop is traversed, the variable saves the maximum value in the array
public static void main(String[] args){
	int[] arr = {5,15,2000,10000,100,4000};
	//Define a variable 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++){
	//Compare the traversed element with the variable max
	//If the array element is greater than max
	if(arr[i]>max){
		//max record the large value
		max=arr[i];
	}
		System.out.println("The maximum value of the array is:"+max);
	}
}

3.5 array inversion

  • Array inversion: the order of elements in the array is reversed. For example, the original array is 1, 2, 3, 4, 5, and the inverted array is 5, 4, 3, 2, 1
  • Implementation idea: the elements at the farthest end of the array exchange positions
    • To achieve inversion, you need to exchange the positions of the farthest elements of the array
    • Define two variables to hold the minimum index and maximum index of the array
    • Element exchange position on two indexes
    • Minimum index + +, maximum index –. Swap positions again
    • The minimum index exceeded the maximum index, and the array inversion operation ended

public static void main(String[] args){
	int[] arr = {5,15,2000,10000,100,4000};
	/*
		Min index of variable min=0 defined in loop
		max=arr.length-1 Maximum index
		min++,max--
	*/
	for(int min=0,max=arr.length;min<=max;min++,max--){
	//Use the third-party variable to complete the element exchange in the array
	int temp=arr[min];
	arr[min]=arr[max];
	arr[max]=temp;
	}
	//After inversion, traverse the array
	for(int i=0;i<arr.length;i++){
		System.out.println(arr[i]);
	}
}

4, Array as method parameter and return value

4.1 array as method parameter

In the previous methods, we learned the parameters and return values of the method, but all the basic data types are used. Can an array as a reference type be passed as a parameter of the method, of course.

  • Array is passed as a method parameter, and the passed parameter is the address of array memory.
public static void main(String[] args){
	int[] arr = {1,3,5,7,9};
	//Call method to pass array
	printArray(arr);
	}
	/*
		Create a method that accepts parameters of array type
		Traverse the array
	*/
public static void printArray(int[] arr){
	for(int i=0;i<arr.length;i++){
		System.out.println(arr[i]);
	}
}

4.2 array as method return value

  • Array is the return value of the method, and the memory address of the array is returned
public static void main(String[] args){
	//Call the method to accept the return value of the array
	//Received is the memory address of the array
	int[] arr=getArray();
	for(int i=0;i<arr.length;i++){
		System.out.println(arr[i]);
	}
}

/*
	Create a method whose return value is an 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;
}

4.3 parameter types of methods

code analysis

1. Analyze the following program codes and calculate the output results
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;
}
2. Analyze the following program codes and calculate the results.
public static void main(String[] args){
 int[] arr = {1,3,5};
 System.out.println(arr[0]);
 Change(arr);
 System.out.println(arr[0]);
}

public static void Change(int[] arr){
	arr[0] = 200;
}

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

Posted by shamuntoha on Sun, 19 Sep 2021 04:26:16 -0700