-1

我的问题是这个

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

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

任何解决方案,这都被 try catch 包围。在这部分解决问题teamArr[counterPopulate] = sf.nextLine();

4

2 回答 2

5

因为ArrayList和普通的不同arrays,你需要使用ArrayList类的方法来填充ArrayList.

在您的情况下,您需要执行以下操作:

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

假设您使用的是 Java。

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

于 2013-10-24T14:19:29.077 回答
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

于 2013-10-24T15:05:16.003 回答