An array is a container object that holds a fixed number of values of a single type.
The length of an array is established when the array is created.
After creation, its length is fixed.
- The Java Tutorial by Oracle
An array is an object of reference type which contains a fixed number of components of the same type;
the length of an array is immutable.
Creating an instance of an array requires knowledge of the length and component type.
Each component may be a primitive type (e.g. byte, int, or double).
Multi-dimensional arrays are really just arrays which contain components of array type.
int[] n = new int[3]; //instantiates an array of 3 integers
//allocates memory for 3 integers
n[0] = 10; //assigns first element at index 0 with value 10
n[1] = 20; //assigns second element at index 1 with value 20
n[2] = 30; //assigns third element at index 2 with value 30
System.out.print(n[0] + " " + n[1] + " " + n[2]); //prints element of the array
//n at specified index location
10 20 30
int[] n = {10,20,30}; //declares and instantiates n array of 3 integers
System.out.print(n[0] + " " + n[1] + " " + n[2]); //prints element of the
//array numbers at index location
10 20 30
int[] scores = new int[4];
for(int i = 0; i < scores.length; i++){
scores[i] = i; //assigns value i to every index of the array
}
for(int i = 0; i < scores.length; i++){
System.out.print(scores[i] + " "); //prints every element in the array
//from index 0 to scores.length - 1
}
0 1 2 3
Random r = new Random(); //instantiates a Random Object
int[] n = new int[5]; //instantiates an array of 5 integers
for(int i = 0; i < n.length; i++){
n[i] = r.nextInt(10-1+1) + 1; //generates random numbers and assigns
//numbers at index i.
}
for(int i = 0; i < n.length; i++){ //for loop to access all indexes of the array
System.out.println(n[i] + " "); //prints each element in the array
//from index 0 to scores.length -1
}
6 9 6 2 5 //array of 5 random integers printed
double[] grades = new double[5]; //instantiates an array of 5 doubles
for(int i = 0; i < grades.length; i++){
System.out.print("Enter grade: " + (i+1 + " ")); //prompts user to enter grade
double g = input.nextDouble();
grades[i] = g; //assigns grade g entered at index i
}
for(double grade: grades){ //uses enhanced for loop
System.out.println(grade); //prints every grade in array grades
}
Enter grade 1: 67 //prompts user to enter first grade, user enters 67
Enter grade 2: 78 //prompts user to enter second grade, user enters 78...
Enter grade 3: 89 . . .
Enter grade 4: 90 . . .
Enter grade 5: 78 . . .
67.0 78.0 89.0 90.0 78.0 //array of grades (double) printed
double highestGrade = 0.0; //initializes a starting highest grade
for(int i = 0; i < grades.length; i++){
if(grades[i] > highestGrade){ //compares current highestGrade to all grades
highestGrade = grades[i]; //assigns new highest grade if grade in array
//is higher than the highestGrade
}
}
System.out.println("The highest grade is:" + highestGrade); //prints the highest grade
90.0 //highest grade is printed