2011-06-08 66 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