# I Advanced application of array

## 1. Array inversion

Meaning: exchange the elements in the array in order of index

Prerequisites for reversal:
1. Confirm the index position of the element to be exchanged
int start = 0;
int end = arr.length - 1
2. The law of index position movement after each exchange
start++
end–
3. How to exchange elements at two index positions
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
4. What conditions are met to exchange elements at two index positions:
When the length of the array is odd: start < end
When the length of the array is an even number: start < end

The code is as follows:

```		//Declare and initialize arrays
int[] arr = {11,22,33,44,55};

System.out.print("Before inversion:[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
System.out.println(arr[i] + "]");
} else {
System.out.print(arr[i] + ", ");
}
}

//Invert the array
for (int start = 0 , end = arr.length - 1 ; start < end ; start++ , end--) {
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
}

System.out.print("After reversal:[");
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
System.out.println(arr[i] + "]");
} else {
System.out.print(arr[i] + ", ");
}
}
```

## 2. Bubble sorting

Bubble sort: in fact, it is to sort the maximum value
Principle: compare two adjacent elements and exchange the larger of the two elements to the right
analysis:
1. Consider that the elements in the array cannot be sorted after traversing once, and all the elements should be traversed multiple times. Select the circular nesting format
2. The loop variable of the outer loop controls "finding the maximum value several times"
3. The loop variable of the inner loop controls "how many times do you need to sort to find the maximum value once" The code is as follows:

```		//Declare and initialize arrays
int[] arr = {5,4,6,8,9};

//Sort the array
/*
When the first round of traversal is carried out: 5-1 judgments are required;
When the second round of traversal is carried out: 5-2 judgments are required;
When the third round of traversal is carried out: 5-3 times of judgment is required;
When the fourth round of traversal is carried out: 5-4 times of judgment is required;
*/
//Outer loop: find the maximum several times
//Core code
for (int i = 1; i < arr.length ; i++) {
//Inner loop: how many judgments are needed to find the maximum value once
for (int j = 0; j < arr.length - i; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}

```

## 3. Select Sorting

Principle: select the smallest element from the data to be sorted in each trip and put it at the end of the ordered sequence until all records are sorted.
Idea: given array: int [] arr = {n data}; The data to be sorted is the smallest in 1 ~ 1
It exchanges with data 1; The second time, select the smallest data from the data 2~n to be sorted and exchange it with data 2; And so on, i
Select the smallest data from the data i~n to be sorted and exchange it with data i until all sorting is completed.

The code is as follows (core code):

```	int[] arr = {5,4,6,8,9};
for (int i = 0; i < arr.length - 1 ; i++ ) {
// The index that records the temporary minimum value of the current number of trips
int index = i;
// Traverse the array to get the index of the temporary minimum value
for (int j = i + 1 ; j < arr.length; j++ ) {
if (arr[index] > arr[j]) {
index = j;
}
}
// If the index position of the temporary minimum changes, the position is exchanged
if (index != i) {
int temp = arr[index];
arr[index] = arr[i];
arr[i] = temp;
}
}
```

## 4. Binary search method of array

Binary search: half fold, half fold and then half fold
Requirement: the array elements must support size comparison, and the elements in the array have been sorted by size The code is as follows (core code):

```	int[] arr = {2,5,7,8,10,15,18,20,22,25,28};//Arrays are ordered
int value = 18;
int index = -1;
int start = 0;
int end = arr.length - 1;
int mid = (start + end)/2;
while(start <= end){
if(value > arr[mid]) {
//Move the left boundary so that the mid moves to the right
start = mid + 1;
} else if(value < arr[mid]){//Keep looking to the left
end = mid - 1;
} else {
index = mid;
break;
}
mid = (start + end)/2;
}
if(index==-1){
System.out.println(value + "non-existent");
}else{
System.out.println(value + "The subscript is" + index);
}
```

# II Comprehensive application of arrays and methods

## 1. Variable parameters

In jdk1 After 5, if we define a method that needs to accept multiple parameters and the types of multiple parameters are consistent, we can simplify it into the following format:

```Modifier return value type method name(Parameter type... Formal parameter name){ }
```

In fact, this writing is completely equivalent to

```Modifier return value type method name(Parameter type[] Formal parameter name){ }
```

Only for the latter definition, the array must be passed when calling, while the former can directly pass data.

The code is as follows:

```public class Demo {
public static void main(String[] args) {
int[] arr = { 1, 4, 62, 431, 2 };
int sum = getSum(arr);
System.out.println(sum);
// 6 7 2 12 2121
// Find these elements and 6 7 2 12 2121
int sum2 = getSum(6, 7, 2, 12, 2121);
System.out.println(sum2);
}
/*
* Complete the original writing method of summing all elements of the array
public int getSum(int[] arr){
int sum = 0;
for (int i = 0 ; i < arr.length ; i++) {
sum += arr[i];
}
return sum;
}
*/
//Variable parameter writing
public static int getSum(int... arr) {
int sum = 0;
for (int i = 0 ; i < arr.length ; i++) {
sum += arr[i];
}
return sum;
}
}

```

Precautions for variable parameters:
1. When declaring a method, there are other parameters in the formal parameter list of the method besides the variable parameters. It is necessary to declare the variable parameters at the last position in the formal parameter list, otherwise the compilation will report an error
2. When declaring a method, the formal parameter list of the method can contain at most one variable parameter, otherwise the compilation will report an error

## 2. Characteristics of formal parameters and actual parameters of the method

Formal parameter: when defining a method, the variable name in parentheses after the method name is called formal parameter (formal parameter for short), that is, the formal parameter appears in the method
Defined.
Argument: when calling another method in the caller's method, the parentheses in the name of the method are called the actual parameters (referred to as the actual parameters), that is, the arguments appear in the caller's method.
Characteristics of formal parameters and actual parameters:
When the formal parameter of the method is a basic data type, the change of the formal parameter value will not affect the actual parameter;
When the formal parameter of the method refers to the data type, the change of the address value of the formal parameter will not affect the argument, but the change of the data in the address value of the formal parameter will affect the argument.

## 3. Dynamic expansion, deletion and insertion of array

(I'll take this out alone)

# III Two dimensional array

Two dimensional array: the elements in the array are still arrays
Declaration of two-dimensional array:

```data type[][] Array name;(recommend)
Data type array name[][];
data type[] Array name[];
```

## 1. Initialization of two-dimensional array

dynamic initialization

```Format 1:While initializing the two-dimensional array,Initialize each one-dimensional array inside
data type[][] Array name = new data type[x][y];
x : How many one-dimensional arrays does the two-dimensional array contain
y : How many elements does each one-dimensional array contain
Format 2:Initializes a two-dimensional array, but does not initialize each one-dimensional array
data type[][] Array name = new data type[x][];
```

initiate static

```Format 1:
data type[][] Array name = new data type[][]{new data type[]{Element 1,Element 2,...,element n},new data type[]{Element 1,Element 2,...,element n},..,new data type[]{Element 1,Element 2,...,element n}}
Format 2:
data type[][] Array name = new data type[][]{{Element 1,Element 2,...,element n},{Element 1,Element 2,...,element n},...,{Element 1,Element 2,...,element n}}
Format 3:
data type[][] Array name = {{Element 1,Element 2,...,element n},{Element 1,Element 2,..,element n},...,{Element 1,Element 2,...,element n}}
```

```format: