Why do we use arrays? When do we need to use arrays?
1. Grammar of arrays:
Be careful:
(1) When defining an array, the length of the space must be given. Once the length of the space is defined, it can not be modified. When the operation exceeds this length, the error of the array subscript crossing the boundary will be thrown.
(2) Once a number of data types is defined, the data types in the array cannot be changed.
(3) The initial value of the array is 0; int - > 0; double - > 0.0; String - > null; char - > space, strong to Int can be seen to be 0;
int[] arr1 = new int[10]; int[] arr2 = new int[]{1,2,3,4,5,6}; //Length 6 char[] arr3 = new char[10]; String[] arr4 = new String[10];
Assign values to arrays:
int [] array: arr [index] = number;
String array: arr [index]= "string"
2. Traversal of arrays
(1) Traditional for cycle
(2) Enhanced for cycle
public class Practice { public static void main(String[] args) { // Create an array arr1 int[] arr1 = new int[] {1,23,45,23,45,32}; // for loop traversal output System.out.println("for:"); for(int i=0;i<arr1.length;i++) { System.out.print(arr1[i]); } //Enhanced for loop traversal output System.out.println("\n Enhance for:"); for(int a:arr1) { System.out.print(a); } } }
Practice:
The requirement is to assign a random number to an array and output its maximum value.
import java.util.Random; public class ergodic { public static void main(String[] args) { int[] arr = new int[10]; int max = arr[0]; for(int a:arr) { a = new Random().nextInt(); System.out.println(a); if(a>max) { max = a; } } System.out.println("Maximum:"+max); } }
Exercise 2:
Rolling table: insert a number into the array that has been sequenced, so that it can be inserted into the corresponding position of size, and then move backwards.
import java.util.Scanner; public class Demo03 { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Please enter a total of 50-90 Numbers between:"); int num = input.nextInt(); // /* * [50,60,70,80,90] * 65 -> 2 * [50,60,65,70,80] * * Core: Subscription operation * */ int[] arr = new int[] {50,60,70,80,90}; //1 Find Subscript int index = 0; //subscript for(int i=0;i<arr.length;i++) { if(arr[i]>num) { index=i; //Record subscript position after comparing sizes break; } } //Move the values in the array backwards and forwards for(int i=arr.length-1;i>index;i--) { arr[i]=arr[i-1]; } //Reassign subscript position arr[index]=num; //Ergodic output for(int a:arr) { System.out.print(a+" "); } } }
Exercise 3: Bubble sorting
import java.util.Arrays; public class Demo04 { public static void main(String[] args) { /* * bubble sort */ int[] arr = new int[] {67,18,21,36,9}; /*Code Steganography Start Location*/ for(int i=0;i<arr.length-1;i++) { for(int j=0;j<arr.length-i-1;j++) { //The purpose of-i is not to compare-1 with the last maximum in the last cycle. //Don't compare the last digit with the last digit, otherwise the array subscript crosses the boundary. if(arr[j]>arr[j+1]) { int temp = arr[j]; arr[j]=arr[j+1]; arr[j+1]=temp; } } } /*Code Steganography Termination Location*/ System.out.println(Arrays.toString(arr)); } }
3. Tool classes for arrays
import java.util.Arrays; public class ArraysTool class methods { public static void main(String[] args) { // Prepare an array of two int classes int[] arr1 = new int[] {1,2,3,4,5,6,4564,6456,34,54}; int[] arr2 = new int[] {543,564,64,7,4564,3,56,64}; //Determine whether the values of arr1 and arr2 are equal - > false System.out.println(Arrays.equals(arr1, arr2)); // Sort arr1 Arrays.sort(arr1); // Output arr1 to see the result - > [1, 2, 3, 4, 5, 6, 34, 54, 4564, 6456] System.out.println(Arrays.toString(arr1)); // Fill arr2 with a value of 9999 Arrays.fill(arr2, 9999); //Output arr2 to see results - > [9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999, 9999] System.out.println(Arrays.toString(arr2)); //Copy arr1 into an array with 13 data spaces. // If the data space is smaller than the original array, the more data in the original array will be cut down; if the data space is larger than the original array, the more data space will be filled with the initial value of 0; int[] newArr = Arrays.copyOf(arr1, 13); // Output newArr Look - > [1, 2, 3, 4, 5, 6, 34, 54, 4564, 6456, 0, 0, 0] System.out.println(Arrays.toString(newArr)); // Using this method, the index is returned, requiring the array to be sorted in ascending order int index = Arrays.binarySearch(arr1, 54); //Print index to see - > 7 System.out.println(index); } }