创建二维数组并用1变量填充第一个数组

时间:2013-06-25 02:09:19

标签: java

我收到错误无法将int转换为int [],有人可以帮忙吗?

//Create array
int [][] studentResults = new int [numStudents][numExams];

//Fill 1st dimension with Student numbers 1 through numStudents
for (int count = 0; count < numStudents; count++)
    studentResults[count][] = count + 1;

2 个答案:

答案 0 :(得分:1)

在Java中,如果要为数组中的条目指定值,则需要指定数组的所有实例。我建议如下:

//Create array
int [][] studentResults = new int [numStudents][numExams];

//This loops through the two dimensional array that you created 
//And fills the 1st dimension with Student numbers 1 through numStudents.
for (int count = 0; count < numStudents; count++)
    for (int exam = 0; exam < numExams; exam++)
        studentResults[count][exam] = count + 1;

从而为每个学生迭代studentResults的每个考试条目。

答案 1 :(得分:1)

因此您需要设置每行第一列的值。我们知道第一列索引是0.所以对于每一行设置数组的0列,如此

for (int count = 0; count < numStudents; count++)
    studentResults[count][0] = count + 1;