Preliminary understanding and use of one-dimensional arrays in Java

Why use an array
When you have too many variables of the same type, we use the array variable for ease of operation and management.
He is actually a combination of a bunch of data placed in an orderly manner.
What is an array
1. Array declaration
identifier
array element
element subscript
element type
element type identifier [element subscript, starting from 0] =
{array element, array element, array element (array length, currently 3)};

or
element type [element subscript, starting from 0] identifier =
{array element, array element, array element (array length, currently 3)};

Example 1:
int scores[]={1,2,3}; //array length 3
int[] scores={1,2,3}; //array length 3
string scores[]={"asd"," asd","asd","asd","asd"}; //array length 5
string[] scores={"asd","asd","asd","asd","asd"}; // array length 5

Example 2:
int scores[]=new int[10]; //Array length 10
int[] scores=new int[10]; //Array length 10
String scores[]=new String[20]; //Array length 20
String[] scores=new String[20]; //Array length 20

Note 1: Only one of the declarations of Example 1 and Example 2 can be used for the same array.
For example int scores[]={1,2,3}; int scores[]=new int[10]; this is wrong.
Note 2: Only one of array length and array element can be written in the declaration.
For example int scores[3]={1,2,3}; int scores[]=new int[3] ={1,2,3}; this is wrong.

2.
When assigning an array to an array, the subscript of the array must be clearly written.
For example
int scores[]=new int[10];
scores[0]=5;
scores[1]=5;
scores[0]= scores[0]*5;
System.out.println(scores[0]+" \t"+ scores[1]);

The result is: 25 5

Common use
of for loop plus keyboard assignment operator
scanner input=new Scanner(System.in);
int scores[]=new scores[10];
int num=0;
for(int i=0;i< scores.length ;i++){ //Assign
scores[i]=input.nextint() in a loop 10 times; //Assign
num+= scores[i] with the keyboard; //Sum of the array data
}
Skill 1: scores.length is equal to the value of the array The length can be used directly to determine the number of loops.

Use the enhanced for loop
scanner input=new Scanner(System.in);
int scores[]=new scores[10];
int num=0;
for(int score:scores){ // 10 assignments
num+= score; / /Sum of array data
}
Skill: for(int score:scores) is an enhanced for statement dedicated to arrays, which means that each loop will assign the value of the array scores to the score starting from the subscript "0".

Common mistakes
1. As long as the declaration is written together with the assignment and the array length, the assignment and the array length must be written one.
int scores=new scores[]; (false)
int scores=new scores[1]; (correct)
int scores=new scores[]{1, 2, 3}; (correct)

2. Array out of bounds
int scores=new scores[2];
scores[0]=1;
scores[1]=1;
scores[2]=1;
scores[3]=1;
Analysis: The array declares two data , so scores[2] and scores[3] should not appear, which is an array out of bounds.

3 The grammar stipulates that all assignments to the declaration and the array must be completed in one statement
int scores[];
scores={1,2,3}; (error)

int scores[]={1,2,3}; (correct)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324612541&siteId=291194637