Three, arrays,
Overview of arrays
A one-dimensional array
One-dimensional array knowledge points
Initialization of an array
The index and length of the array
Through the array
The default value of the array (declared and not assigned, print the default value of the array)
Memory diagram:
Memory parsing:
Multidimensional array
Two-dimensional array knowledge
Declaration and initialization of a two-dimensional array
The index and length of a two-dimensional array
A two-dimensional array traverses two levels of the for loop
The initial value of a two-dimensional array
Array memory parsing:
Common algorithms in arrays
The data structure
- Yang hui triangle
public static void main(String[] args) {
int[][] yangHui = new int[10] [];for (int i=0; i<yangHui.length; i++){ yangHui[i] =new int[i+1];
}
yangHui[0] [0] =1;
for (int i=1; i<yangHui.length; i++){ yangHui[i][0] =1;
yangHui[i][i]=1;
for (int j=1; j<i; j++){ yangHui[i][j]=yangHui[i-1][j-1]+yangHui[i-1][j];
}
}
System.out.println("- Print Yang Hui triangle -");
for (int i=0; i<yangHui.length; i++){for (int j=0; j<yangHui[i].length; j++){ System.out.print(yangHui[i][j]+"\t"); } System.out.println(); }}Copy the code
- Find the sum, maximum, minimum, and average of the array
public static void main(String[] args) {
// Random number group
// Define array, sum, min, Max, avg
int[] arr = new int[10];
int sum=0;
int min,max;
float avg;
// Attach a random value to the array
for (int i=0; i<arr.length; i++) {// Assign a value between 1 and 100 to the element
arr[i] = (int) ((Math.random() * 90) + 10);
/ / o combined
sum +=arr[i];
}
// Take the average
avg = sum/10f;
// Select the maximum value and minimum value
min = arr[0];
max = arr[0];
for (int i=0; i<arr.length; i++){if (arr[i]>=max){
max = arr[i];
}
if(arr[i]<=min){ min = arr[i]; }}// Output the result
System.out.println("Array:"+Arrays.toString(arr));
System.out.println("Sum:"+sum);
System.out.println("Average:"+avg);
System.out.println("Maximum value:"+max);
System.out.println("Minimum:"+min);
}
Copy the code
- Array copy
public static void main(String[] args) {
int[] arr1 = new int[] {2.3.5.7.11.13.17.19};
System.out.println(Arrays.toString(arr1));
// Assign the address of arR1 to arR2
int[] arr2 = arr1;
for (int i=0; i<arr2.length; i++){if (i%2= =0){
arr2[i]=i;
}
}
System.out.println(Arrays.toString(arr2));
System.out.println(Arrays.toString(arr1));
Copy the code
int[] arr2 = arr1; Instead of copying the array, you’re assigning the address of ARR1 to ARR2
Copying an array starts by creating a new array
Copy array:
public static void main(String[] args) {
int[] arr1 = new int[] {2.3.5.7.11.13.17.19};
System.out.println(Arrays.toString(arr1));
// Copy the array
int[] arr3 = new int[arr1.length];
for (int i=0; i<arr1.length; i++){ arr3[i]=arr1[i]; }for (int i=0; i<arr3.length; i++){if (i%2= =0){
arr3[i]=i;
}
}
System.out.println(Arrays.toString(arr1));
System.out.println(Arrays.toString(arr3));
}
Copy the code
- Array inversion
public static void main(String[] args) {
// Array inversion
String[] nickname = new String[]{"JJ"."DD"."MM"."GG"."SS"."AA"};
// Define an intermediate number for data exchange
String temp;
for (int i=0; i<nickname.length/2; i++){ temp = nickname[i]; nickname[i]=nickname[nickname.length-1-i];
nickname[nickname.length-1-i]=temp;
}
System.out.println(Arrays.toString(nickname));// output: [AA, SS, GG, MM, DD, JJ]
}
Copy the code
- Array lookup (linear lookup, binary lookup)
Linear search: go down one by one
public static void main(String[] args) {
// Search (or search)
// Linear search: search down one by one
String[] nickname = new String[]{"JJ"."DD"."MM"."GG"."SS"."AA"};
// Check whether the identity of the same element is found
boolean isFlag = true;
String desc ="DD";
for (int i=0; i<nickname.length; i++){if (desc.equals(nickname[i])){
System.out.println("Found the same element, subscript"+i);
isFlag = false;
break; }}if (isFlag==true){
System.out.println("There are no identical elements.");
}
Copy the code
Binary lookup (prerequisite: the array to be searched must be ordered)
public static void main(String[] args) {
// binary search
// Prerequisite: The array to be searched must be ordered
int[] numbers= new int[] {-23, -13, -4.2.9.34.99.100}; // From smallest to largest
int target=99;
// The initial first index value
int head = 0;
// The final index value at the beginning
int end = numbers.length-1;
boolean isFlag1 = true;
while (head<=end){
int mid = (head+end)/2;
if (numbers[mid] == target){
System.out.println("Same element found, subscript:"+mid);
isFlag1 = false;
break;
}else if (numbers[mid]>target){
end = mid -1;
}else {//numbers[mid]<target
head = mid+1; }}if (isFlag1){
System.out.println("There are no identical elements."); }}Copy the code
- Sorting algorithms in array elements
Bubble Sort (write code)
public static void main(String[] args) {
// Bubble sort
int[] numbers= new int[] {-23.0, -4.2, -9.134.99.100};
// When two adjacent numbers are compared, the larger number moves backwards
for (int i =0; i<numbers.length-1; i++){for (int j=0; j<numbers.length-1-i; j++){if (numbers[j]>numbers[j+1]) {int temp = numbers[j];
numbers[j] = numbers[j+1];
numbers[j+1] = temp;
}
}
}
System.out.println(Arrays.toString(numbers));
}
Copy the code
Quicksort (Understanding the mind)
Comparison of sorting algorithm performance
The Use of the Arrays utility class
public static void main(String[] args) {
// Compare whether two arrays are equal
int[] arr1 = new int[] {-23.0, -4.2, -9};
int[] arr2 = new int[] {-23.0, -4.2, -9};
boolean isEquals = Arrays.equals(arr1, arr2);
System.out.println(isEquals);
// Outputs the array information
System.out.println(Arrays.toString(arr1));
// Populates the array with the specified value: each element is replaced with value
Arrays.fill(arr1,15);
System.out.println(Arrays.toString(arr1));
/ / sorting
Arrays.sort(arr2);
System.out.println(Arrays.toString(arr2));
// Binary search, if ordered
int[] arr3 = new int[] {-23, -13, -4.2.9.34.99.100};
int index = Arrays.binarySearch(arr3,32);// A negative number means there is no such number
System.out.println(index);
}
Copy the code
Common exceptions in array usage
Null pointer: The address value is null and does not point to the heap