我需要一點幫助。我想要閱讀如下所示的文本文檔:Java - 從文件讀取和拆分
4 3 2
3 6 9
這不是主要部分,因爲我可以這樣做。問題在於分裂。我如何才能在完全分離的Arraylists中得到這些數字? 我的意思是,如果我有三個Arraylist,我想將第一個數字保存到第一個ArrayList,第二個數字保存到第二個Arraylist,第三個數字保存到第三個Arraylist。
有人可以幫我嗎?
我需要一點幫助。我想要閱讀如下所示的文本文檔:Java - 從文件讀取和拆分
4 3 2
3 6 9
這不是主要部分,因爲我可以這樣做。問題在於分裂。我如何才能在完全分離的Arraylists中得到這些數字? 我的意思是,如果我有三個Arraylist,我想將第一個數字保存到第一個ArrayList,第二個數字保存到第二個Arraylist,第三個數字保存到第三個Arraylist。
有人可以幫我嗎?
這裏有一個小指南您:
你必須找到一種方法來逐行讀取文件中的行。當你這樣做,你可以split(String regex)他們並將結果數組轉換爲List
。
//Homework:
//Add source code that reads the file line by line.
//For each of the read lines, do:
String[] split = line.split(" ");
List<String> myList = Arrays.asList(split);
首先你看這裏String.split()然後你移動到Arrays.asList。
如果我們考慮文件gona只有兩行和三個數字嘗試這樣做。我無法測試這個atm,但應該工作。
ArrayList<Integer> FirstList = new ArrayList();
Arraylist<Integer> SecondList = new ArrayList();
ArrayList<Integer> ThirdList = new ArrayList();
//add add file to read bufer
Bufferedeader buff = new Bufferedreader(FileBuffer(filePath));
//since we know how many numbers we have no need for lopps
FirstList.add(buff.read());
buff.read();
SecondList.add(buff.read);
buff.read();
//and so on
這是definatly不是解決問題的最佳解決方案,但可能工作了,甚至幫助你。
我們可以,但我們首先想看看你已經嘗試過了。 –
我預見你將來會有一臺掃描儀 –