2013-10-24 80 views
-1

我的问题是这样的如何在while循环填充数组列表

Scanner sf = new Scanner(f); 
ArrayList<String> teamArr = new ArrayList<String>(); 
int counterPopulate = 0; 

while(sf.hasNextLine()){ 
    teamArr[counterPopulate] = sf.nextLine(); 
    counterPopulate++;   
} 

的任何解决方案,这是一个尝试捕捉包围。 获取问题,在这部分teamArr[counterPopulate] = sf.nextLine();

+0

语言是什么呢? –

+0

因为'字符串'是大写的,它看起来像java –

+0

你得到一个'索引越界'异常吗?如果没有,你会得到什么例外 –

回答

1

当您使用ArrayList<String>add(String value)方法来添加新的String对象到ArrayList

下面给出了一个简单的问题解决方案。

假设语言是JAVA。

Scanner sf = new Scanner(f); 
ArrayList<String> teamArr = new ArrayList<String>(); 

while(sf.hasNextLine()) { 
    teamArr.add(sf.nextLine()); 
} 

为更多细节ArrayListCollection请参考:

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

http://docs.oracle.com/javase/7/docs/api/java/util/Collection.html