Reputation: 35
2DI need to use a two-dimensional array of doubles to store grades. The first dimension of the array will represents each student while the second dimension represents the grade for each assignment. The maximum number of assignments for any course is provided when the course is created. I need to make it so that grades that have not been assigned are given an initial value of -1
I know that for single arrays you can do this
double[] grade = new double[10];
for (double i = 0; i < size; i++) {
array[i] = -1;
}
How would I do it for a 2D array?
Upvotes: 2
Views: 5667
Reputation: 136022
try this
double[][] grade = new double[10][10];
for (double[] e : grade) {
Arrays.fill(e, -1);
}
Upvotes: 6
Reputation: 209004
First all, you can't do this
double[] grade = new int[10];
double[] and int[] are incompatible types.
To declare a 2D array just use two sets of square brackets
double[][] grade = new double[10][10];
This will give you a total of 100 indices, max index being [9][9], and min[0][0].
To iterate through the array you use a nested loop
for (int i = 0; i < grade.length; i++){ // iterates each student
for (int j = 0; j < grade[i].length; j++){ // iterates each grade
// do something with grade[i][j]
}
}
Upvotes: 2