如何使用一維數組來存儲二維數組的一維? 這是我迄今爲止,我只是不能讓1D數組存儲任何二維數組。2維陣列
編輯:循環正常工作。我只需要製作2個一維數組來存儲2維數組的每個維度。
int[][] tutorData = { // students per day (MTW) per tutor
{25, 3, 0}, // Amy
{14, 5, 12}, // John
{33, 22, 10}, // Nick
{0, 20, 5}}; // Maria
int numOfDays = tutorData[0].length; // number of days = number of "columns"
int numOfTutors = tutorData.length; // number of tutors = number of "rows"
int[] sumPerDay = {tutorData[i]};
sumPerDay = new int [numOfDays]; // array to store total # of students per day
int[] sumPerTutor = {tutorData[j]};
sumPerTutor = new int[numOfTutors]; // array to store total # of students per tutor
for (int i = 0; i < tutorData.length; i++) {
for (int j = 0; j < tutorData[i].length; j++) {
if (j == 0) {
System.out.println("Tutor " + (i + 1) + " met: " + tutorData[i][j] + "students on (M) ");
}
if (j == 1) {
System.out.println("Tutor " + (i + 1) + " met: " + tutorData[i][j] + "students on (T) ");
}
if (j == 2) {
System.out.println("Tutor " + (i + 1) + " met: " + tutorData[i][j] + "students on (W) ");
}
System.out.println();
}
謝謝,幫助了很多! – Indyvette