我想創建一個數組,它將允許我使用存儲的元素來創建另一個單獨的數組。是否可以用數組聲明一個數組?
這是我到目前爲止有:
Scanner reader = new Scanner(System.in);
//variables
int NumberOfStudents;
System.out.print("How many students are in the class?: ");
NumberOfStudents = reader.nextInt();
reader.nextLine();
//objects
String [] names = new String[NumberOfStudents]; //Creates an array based on the number of students
//input
for (int i = 0; i < NumberOfStudents; i++){
System.out.print("What is student number " + (i+1) + "'s name?: ");
names[i] = reader.nextLine();
double [] names[i] = new double [5]; //declares each student name as a separate array
}
在此,我也行double [] names[i] = new double [5];
,應採取names[]
陣列的價值指數i
的,把它變成長度5的陣列。因此,如果names[1] = Ann
,它應該創建一個長度爲5的數組Ann[]
。但是,它會引發表達式錯誤的非法開始。
我試圖使用一個臨時變量,以協助宣佈多個陣列過,但是我獲得了更多的錯誤表達旁邊的非法啓動。因此顯然你不能使用數組或變量來聲明其他數組。
有沒有辦法解決這個問題,而不使用多維數組?
在此先感謝。
如果您講解五行陣的目的,我們也許能夠給你一些選項。 –
@AMACB提問者特別試圖避免使用多維數組。 –
如果你能解釋你認爲這個陳述的作用會有多大幫助:'double [] names [i] = new double [5];',特別是你明白'double'的意思。 –