Continue with our array of shares.
Once we have defined an array, there are many variables of this type in the array, and each element in the array is that type of variable, and we access them by index or by subscript to access, index or subscript must be an integer, starting from 0.
If we say we define I, int i; We get a place called I, where we can put some numbers, if i = 5; then we put 5 in.
Now for example there is an array int[] a = new int[10]; This time we are not a place, we have 10 such places, each place is an int, each place has a number, this number starting from 0, No. 0, 1th 、... Until number 9th, if we say a[0] = 5; So, we put a 5 in place No. 0.
An array is such a thing, it is a lot of one type of variable, each variable is accessed by a numeric number, the number is starting from 0, until the number of arrays-1, the largest subscript is the number of numbers-1. A valid subscript for an array starts with 0, the smallest is 0, and the largest is the number of arrays-1. However, our compilers do not check for valid subscripts. Like what:
We previously defined the numbers array, and now we write numbers[-1] = 10; The compiler did not error;
Write another numbers[100] = 10; We know that 100 has been exceeded, the maximum is only 99, or no error, the compiler does not check this thing. But if we run this program, immediately he throws an exception:
This exception is called an array whose index overflows the boundary, in line 11th.
The front of this program we say may involve a lot of numbers will be out of bounds, is dangerous, because we created 100 of such types of arrays, CNT has been +1, if not a certain limit CNT will be more than 100, so as long as CNT more than 100, our program will throw an exception, We do not know in advance how many numbers the user will enter, just use-one sign to enter the end, directly define 100 to do so is dangerous, how to do is safe.
Before we mentioned that we didn't have to give a constant when we created the array, the size of the array could be a variable, which means we could change the idea that we're not going to let the user enter a 1 to mark the end, but instead let the user enter a number and let the user tell us what number he's going to enter this time. We then decide how big the array should be based on the number and then read the data in. The following is the code we have done with this feature:
Packageexercise;ImportJava.util.Scanner; Public classAverage { Public Static voidMain (string[] args) {//TODO Auto-generated method stubsScanner in =NewScanner (system.in); System.out.print ("Please enter the number of data you want to enter:"); Doublesum = 0; intCNT =In.nextint (); if(CNT > 0) { int[] numbers =New int[CNT]; for(inti = 0;i < cnt;i++) {Numbers[i]=In.nextint (); Sum+=Numbers[i]; } DoubleAverage = sum/CNT; for(inti = 0; i < CNT; i++) { if(Numbers[i] >=average) {System.out.println (numbers[i]); }} System.out.println ("The average is:" +average); } }}
The result of the program operation is:
Please enter the number of data you want to enter: 10
1 2 3 4 5 6 7 8 9 10
6
7
8
9
10
The average is:5.5
Java programming Base Array-3