如何在while循环中填充数组列表

Hri*_*toM -1 java arraylist while-loop

我的问题是这个

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

while(sf.hasNextLine()){
    teamArr[counterPopulate] = sf.nextLine();
    counterPopulate++;           
}
Run Code Online (Sandbox Code Playgroud)

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

Kak*_*kia 5

因为ArrayList与正常情况不同arrays,您需要使用ArrayList类的方法来填充ArrayList.

在您的情况下,您需要做:

while(sf.hasNextLine()){
            teamArr.add(sf.nextLine());
        }
Run Code Online (Sandbox Code Playgroud)

假设你正在使用Java.

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