Array Knowledge Point Arrangement

Posted by suave4u on Tue, 07 May 2019 13:30:03 +0200

array

Hello! This chapter mainly collates the knowledge points related to arrays. If you want to master the basic grammar of arrays or better understand arrays, you can read this article carefully.

Why learn arrays?

Scenario introduction: After the Java exam, the teacher assigned Lao Wang a task to calculate the average score of the class (300 people).
Solution: Define 300 variables, then add and sum them, and calculate the average score.
Technical flaws: redefining variables repeatedly
Arrays can be used to improve programs, as shown in the following programs:

```/**
* Needs: Calculate the average score of the class (300 people).
* @author Xyr
*/
public class MyArray {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

// Defined array
double[] scores = new double[30];

double sum = 0.0;
double average = 0.0;

for (int i = 0; i < scores.length; i++) {
System.out.print("Please input number 1." + (i+1) + "Achievements of individual students:");
scores[i] = input.nextDouble();
// Summation summation
sum += scores[i];
}

average = sum / scores.length;

System.out.println(scores.length + "The average score of each student is:" + average);

input.close();
}
}
```

The Concept of Array

An array is an array container for storing a set of variables of the same data type.
The characteristics of arrays:
1. Array is essentially a variable of reference type. Since it is a variable, it must be declared, then assigned, and then used.
2. Array element types are determined at compile time and can only store a set of data of the same data type.
3. Arrays create a contiguous set of memory spaces in the heap area, with the array header address as the memory address of the array.

Initialization of arrays

Static initialization: Initialization gives the initial value for each element at the same time, without specifying the length of the array, the system will dynamically calculate the length of the array according to the number of elements.

```	Format 1: Data type [] array name = {element 1, element 2, element 3,... Element n};
Format 2: Data type [] array name = new int []{element 1, element 2, element 3,... Element n};
```

Dynamic initialization: The system assigns an initial value to the array, but we must specify the length of the array.
The default rule is as follows

```		byte short int long default value 0
float double default value 0.0
The default value of char '0000' is an empty character.
boolean default false
Reference type default value null
```

Array Notes

```public class ArrayDemo01 {
public static void main(String[] args) {
int[] arr = new int[3];
System.out.println(arr); // [I@7852e922

System.out.println(arr[0]); // 0
System.out.println(arr[1]); // 0
System.out.println(arr[2]); // 0
System.out.println(arr[-3]); //  java.lang.ArrayIndexOutOfBoundsException: 3

int[] arr2 = null;
System.out.println(arr2[1]); // java.lang.NullPointerException
}
}
```

The memory diagram is as follows:

Anomaly resolution:

```/*
*	java.lang.ArrayIndexOutOfBoundsException: 3
* Exception name: array crossover
* Cause: Access array subscripts are not between 0 and array length-1
* Solution: Scope of inspection
*
*  java.lang.NullPointerException
* Exception name: null pointer exception
* Cause: Object members are accessed without new
* Solution: Find null value objects and create objects
* /
```

Traversal of arrays

```public class ArrayDemo03 {
public static void main(String[] args) {

int[] arr = { 11, 22, 33, 44, 55 };

// One way
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]);
System.out.println(arr[5]);

// Mode two
for (int i = 0; i < 6; i++) {
System.out.println(arr[i]);
}

// The array has only one property: the length of the length array
System.out.println(arr.length);

// Mode 2: Improvement of Array Length Attribute
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}

// Mode 3: Method improvement
printArr(arr);
System.out.println(arrayToString(arr));

// The system provides a toolkit for traversing arrays Arrays
System.out.println(Arrays.toString(arr));

}

public static void printArr(int[] arr) {
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}

// Print format: [11, 22, 33, 44, 55]
public static String arrayToString(int[] arr) {
String result = "";

result += "[";

for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
// If it's the last element, splice the data
result += arr[i];
} else {
// If it's the last element, splice data +,_
result += arr[i] + ", ";
}
}

result += "]";

return result;

}

// How the source Arrays tool class overrides the toString method can be learned for reference
public static String toString(int[] a) {
if (a == null)
return "null";
int iMax = a.length - 1;
if (iMax == -1)
return "[]";

StringBuilder b = new StringBuilder();
b.append('[');
for (int i = 0; ; i++) {
b.append(a[i]);
if (i == iMax)
return b.append(']').toString();
b.append(", ");
}
}
}
```

On Value Passing and Reference Passing in Arrays

```/*
* Transfer process when formal parameters are reference types
*
* 	Eight Basic Type Transfer Satisfies Type Conversion
*  reference type
*  	array
*  	class
*  	Interface
*
*  Value passing and reference passing
*  	1.The essence of value passing is the value itself, and the essence of reference passing is the address.
*  	2.If the address is passed, then changing the value of the heap space through the address will affect all references to the heap area.
*/
public class ArrayDemo04 {
public static void main(String[] args) {
int a = 10;
System.out.println("Pre-method call parameters a Value: " + a);  // 10
change(a);
System.out.println("Parameters after method invocation a Value: " + a); //  10

int[] arr = {11, 22, 33};
System.out.println("Values of all elements in the preceding array of method calls: " + Arrays.toString(arr)); // [11, 22, 33]

change(arr);
System.out.println("Values of all elements of the array after method invocation: " + Arrays.toString(arr));  // [11, 300, 22]
}

public static void change(int a) {
a += 5;
System.out.println("Parameters in the method a Value: " + a); // 15
}

public static void change(int[] arr) {

arr[1] = 300;
System.out.println("Values of all elements of an array in a method: " + Arrays.toString(arr)); // [11, 300, 33]
}
}
```

The value-passing memory diagram is as follows:

The reference transfer memory diagram is as follows:

```import java.util.Arrays;

public class ArrayDemo {
public static void main(String[] args) {
int[] arr = {11, 22, 33}; // [11, 200, 33]
int[] arr2 = {44, 55, 66}; // [44, 55, 66]

arr[1] = 200;
int[] arr3 = arr; // [11, 200, 33]

System.out.println(Arrays.toString(arr)); // [11, 200, 33]
System.out.println(Arrays.toString(arr2)); // [44, 55, 66]
System.out.println(Arrays.toString(arr3)); // [11, 200, 33]

arr3[2] = 400;
System.out.println(Arrays.toString(arr)); // [11, 200, 400]
System.out.println(Arrays.toString(arr2)); // [44, 55, 66]
System.out.println(Arrays.toString(arr3)); // [11, 200, 400]
}
}
```

Topics: Java Attribute