A. What is an array:
An array is a variable that stores a set of data of the same data type
Declaring a variable is an appropriate space in the memory space.
Declaring an array is a sequence of contiguous spaces in the memory space.
Two. Basic elements of the array:
Identifier: The name of the array used to distinguish between different arrays
Array elements: Data stored in the array
Element subscript: The array element is numbered, starting with 0, and each element in a set can be accessed by subscript
Element type: The data type of the array element
Note: Array lengths are fixed to avoid array out of bounds
Three: Steps to use an array:
1. Declaring an array: Tell the computer what the data type is int [] A;
Grammar:
Data type array name [];
data type [] array name;
2. Allocate space: Tell the computer to allocate several contiguous spaces a=new int[5];
Grammar:
data type [] Array name = new data type [size];
3. Assignment: A[0]=8 The data to the allocated lattice;
3.1 Edge Declaration Edge Assignment
Int[] Score = {89, 79, 76};
Int[] score = new int[]{89, 79, 76}; Cannot specify array length
3.2 Dynamically input information from the keyboard and assign values
Scanner input = new Scanner (system.in);
for (int i = 0; i <; i + +) {
Score[i] = Input.nextint ();
}
4. Processing data: Calculate the average a[0]=a[0]*10 of 5 students;
int [] score = {60, 80, 90, 70, 85};
Double avg;
Avg = (Score[0] + score[1] + score[2] + score[3] + score[4])/5;
The array name is the length of the array.
Example: Calculating the average of a class student
public class Demo01 {
public static void Main (string[] args) {
Store 30 students ' scores
int [] score=new int[5];
Double avg=0.0;//Average Score
Double sum=0;//result sum
Scanner input=new Scanner (system.in);
. Length: Represents the length of the array 30
for (int i = 0; i < score.length; i++) {
System.out.println ("Please enter" + (I+1) + "Participant's score:");
Score[i]=input.nextint ();
Calculates the sum of each iteration
Sum=sum+score[i];
}
Avg=sum/score.length;
System.out.println ("Mean score:" +AVG);
}
}
Four. Array sorting:
Ascending: Arrays.sort (array name); from 0 to Maximum
Descending: Up to 0 after the row is finished in ascending order
Case: Ascending He descending score
public class Demo02 {
public static void Main (string[] args) {
Defines an array that stores the scores of 5 learners
int [] Score=new int[]{98,56,74,85,100};
SYSTEM.OUT.PRINTLN ("Initial results of 5 participants:");
for (int i = 0; i < score.length; i++) {
System.out.print (score[i]+ "\ t");
}
System.out.println ();
SYSTEM.OUT.PRINTLN ("Scores of 5 trainees in ascending order:");
Ascending row
Arrays.sort (score);
for (int i = 0; i < score.length; i++) {
System.out.print (score[i]+ "\ t");
}
System.out.println ();
SYSTEM.OUT.PRINTLN ("5 students in descending order after the score:");
Descending: printing from backward forward
Score.length-1: Array Max subscript
for (int j = score.length-1; j>=0; j--) {
System.out.print (score[j]+ "\ t");
}
}
}
Java new gang--array