1.basic use of arrays
(1) the function of the array
A set of numbers of the same data type
An array is also a variable, a string of contiguous spaces in memory
(2) Basic concepts of arrays
Elements: (data stored in an Array)
data type: (type of data,int,double,string , etc.)
subscript (starting from 0 to array . length-1 End)
the length of the array (array . Length)
(3) array Use steps
Declaring an array
int[] num;
Int num[];
Allocate space
Num=new int[9];
Assign Value
int[] num={5,8,9};
Working with Data
Sum of Steps:
int[] num=new int[]{5,8,9};
(4) duration:
(1.1) method one: Traditional for
(1.2) method Two:
Enhanced for(foreach)
for (int score:scores) {
sum+=score;
}
(can only be used in an array of well-assigned values, assigning values obtained in scores to score)
the purpose is to iterate through each value in the Array. The traversal means: every value goes through it again.
If you use Scanner words, The following wording:
for (int I=0;i<scores.length;i++) {
System. Out. println ("please enter" + (i+1) + "gate score");
Scores[i]=input.nextint ();
}
for (int score:scores) {
sum+=score;
}
(5) Common Errors:
correction: such as: (1)int score""={60,80,90,70,85};
(2)int score""=new int"" {60,80,90,70,85 };
(6)Example Explanation:
and a for is used toiterate over it so that each number in the array is compared to the value entered
(7) Basic use of arrays
declaring an array (type [] array name or type array name [])
Allocate Space ( what is the value of the length of the declared array )
(array Name = new data type [X]; )
Attention:
created by New , typically reference data types
Assign value (num0= ; num1= ; )
Process data (process data according to specific requirements)
Variable:
1. Basic data type: (stored in stack memory; no METHOD)
int ,float,double, char, boolean
2, Reference Data type: (stored in the heap memory, you can point out methods and properties)String, Array
3. Basic data types and reference data types:
The memory distribution is different,
different on Use (reference data type can . out some methods and properties)
Java--array