Array is a variable that stores a set of data of the same data type (that is, a data type that can store many numeric values)
If declaring a variable is an appropriate space in the memory space, declaring an array is a contiguous set of spaces in the memory space.
Basic Requirements for arrays
- Identifier: The name of the array used to distinguish between different arrays
- Array elements: Data stored in an array
- Element subscript: Starting from 0
- Element type: The data type of the array
Basic steps for array manipulation
1) Declaring an array: Tell the computer what the data type is
data type [] array name; such as: int[] score1;
Data type array name []; such as: int score2[];
Note: The length of an array cannot be declared when declaring an array
2) Allocate space: Tell the computer to allocate several contiguous spaces
such as: Score=new int[30];
Data type [] new data type [size];
3) Assign Value
Edge Declaration Variable assignment value
Int [] score=new int[]{89,79,69}; There are 3 values in front of 3, the computer does not allow
Int[] score={89,69,79}
You can also use console input to assign values
Scanner input =new Scanner (system.in); for (int i=0; i<30; i++) {score[i]=input.nextint ();}
Example:
/** Define an array sequence * The value of the loop output sequence number of all values in the series and * guessing games: Enter any data from the keyboard to determine if the number is included in the sequence*/ Public classTest01 { Public Static voidMain (String args[]) {int[] arr; Arr=New int[]{8,4,2,1,23,34,12}; intSum=0; for(inti=0;i<arr.length;i++) {System.out.println (arr[i]); Sum=sum+Arr[i]; } System.out.println ("The sum of the array sequence is:" +sum); Scanner input=NewScanner (system.in); System.out.println ("Please enter a number:"); intj=Input.nextint (); intK=0; for(inti=0;i<arr.length;i++){ if(j==Arr[i]) {k=1; Break; } } if(k==1) {System.out.println (Contains); }Else{System.out.println ("Does not contain"); } }}
Java Array Chapter