我有一個輸入文件名爲「animals.txt」:從文本文件中讀取數據,並建設有數組表
sheep 10.5 12.3 4
horse 8.4 11.2 7
cow 13.7 7.2 10
duck 23.2 2.5 23
pig 12.4 4.6 12
把我的問題,簡單地說,我想知道如何存儲4將數據列從輸入文件分成4個獨立的1維數組。
輸出應該像這樣的事情...
[sheep, horse, cow, duck, pig]
[10.5, 8.4, 13.7, 23.2, 12.4]
[12.3, 11.2, 7.2, 2.5, 4.6]
[4, 7, 10, 23, 12]
到目前爲止,我已經想通了如何將所有數據存儲到一個大陣,但我需要知道如何打破下來,而是將每列存儲到自己的數組中。
我的代碼:
public static void main(String[] args) throws FileNotFoundException {
String[] animal = new String[5];
int index = 0;
File file = new File("animals.txt");
Scanner input = new Scanner(file);
while (input.hasNextLine() && index < animal.length) {
animal[index] = input.nextLine();
index++;
}