2011-06-08 69 views

回答

5

除非您事先知道文件中的行數,否則我建議您收集兩個Lists中的數字,例如ArrayList<Integer>

像這樣的東西應該做的伎倆:

List<Integer> l1 = new ArrayList<Integer>(); 
List<Integer> l2 = new ArrayList<Integer>(); 

Scanner s = new Scanner(new FileReader("filename.txt")); 

while (s.hasNext()) { 
    l1.add(s.nextInt()); 
    l2.add(s.nextInt()); 
} 

s.close(); 

System.out.println(l1); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 
System.out.println(l2); // [12, 15, 6, 4, 3, 6, 12, 8, 8, 9, 13] 

如果你真的需要在兩個int[]陣列中的號碼,你以後可以創建數組(當尺寸是已知的)。

+0

不錯的答案:)! OMG! – Bohemian 2011-06-08 10:11:44

+0

你是傳奇。 – Mehdi 2011-06-08 11:27:59

+0

@Mehdi,hehe,no [not yet](http://stackoverflow.com/badges/146/legendary) – aioobe 2011-06-08 11:28:37