我希望將文本文件中的內容存儲在數組中。以下是txt文件的數據:JAVA:將內容從txt文件存儲到3D數組
0.8585781857237149 0.27817454182457335 -0.8050499953993335
0.6370714882668496 0.2972334455862271 -0.03239256370254662
-0.27150466294617615 0.6458147357741209 -0.8755197569879973
0.8714523367008264 0.5051711395439467 0.7632793840501568
0.9722198583553305 -0.6540230961515898 0.5498519669064881
-0.1289712393377327 0.5729094349133539 -0.32452314324200193
我試圖執行此代碼:
FileInputStream in = new FileInputStream("file.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
while ((line = br.readLine()) != null) {
for (z=0; z<2; z++){
for (y=0; y<9; y++){
for (x=0; x<5; x++){
filearray[x][y][z]=br.readLine();
}
}
}
System.out.println(Arrays.toString(filearray));
}
但輸出給了我一個錯誤這樣的錯誤消息:
[[[Ljava.lang.String;@2a3046da, [[Ljava.lang.String;@2a098129, [[Ljava.lang.String;@198e2867, [[Ljava.lang.String;@12f40c25, [[Ljava.lang.String;@3ada9e37]
我的代碼中是否有任何錯誤?提前致謝。
確定這是一條錯誤消息嗎?我相信它實際上是你正在打印的輸出 –
「_the輸出給了我一個錯誤_」這不是一個錯誤,這是從數組輸出的有效'toString()'。 – csmckelvey
嘗試打印該數組中的特定對象。如果您嘗試打印整個數組,它總是向它提供一個HashCode。不是直接的價值。 嘗試System.out.println(Arrays.toString(filearray [x] [y] [z]));在你的for循環中。 – procrastinator