This week I learned about arrays, and here I will do some summary
1. First declare an array:array type [] array name
2. Define the array storage space:array name = new data type [number of spaces];
Combining the first two can directly declare an array: for example int number=new int[10];//can store 10 elements of an integer array
3. Assign a value to the array:array name [index position]= value;Index position and array subscript int[10]=1;10 and subscript, also index position.
If it is too much to declare a step, there can be an array declaration, allocation of space, the assignment of three to one
int [] number=new int[]{9,3,4,5,2,5,2,5};//But notice [] the number of non-writable spaces, which is the length.
For the application of arrays, the first is to find the maximum and minimum numbers from the array
Double max = number[0];//defines the maximum Max, assigns the first digit of the number array to Max
Double min = number[0];//defines the minimum min, assigns the first digit of the number array to min
for (int i = 0; I <= 5; i++) {//Loop 6 times, add one at a time
if (Number[i] >= max) {//If the number of digits in the array is greater than or equal to the first digit, then the I-bit number is assigned to Max, and the i+1 bit and the I-bit are compared after the loop, and if it is greater than or equal, it is assigned to Max
max = Number[i];
}
if (Number[i] <= min) {//similarly
min = Number[i];
}
}
and sort the array.
int x, y; Defining integral type X, y
for (x=0;x<8;x++) {//X Loops 7 times, X is the subscript of an array, first position is the first number of arrays
for (y=x+1;y<8;y++) {//First array second number
if (Number[x]>number[y]) {//compares the first and second bits, if the x>y is exchanged, and so on, it is eventually sorted.
T=NUMBER[X];
Number[x]=number[y];
number[y]=t;
}
}
If the above sort of trouble, there is a simple command:arrays.sort (array name);Default Ascending
The above is my study and summary of this week, if there are shortcomings, welcome to correct and supplement.
Java Array Basics