如何在while循环中填充数组列表 [英] How to populate an array list in a while loop
本文介绍了如何在while循环中填充数组列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的问题是这个
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();
Any solutions, this is surrounded by a try catch.
Getting the problem at this part teamArr[counterPopulate] = sf.nextLine();
推荐答案
因为ArrayList
与普通的arrays
不同,需要使用ArrayList的方法
类来填充 ArrayList
.
Because ArrayList
is different than normal arrays
, you need to use methods of the ArrayList
class to populate the ArrayList
.
在你的情况下,你需要做:
In your case you need to do:
while(sf.hasNextLine()){
teamArr.add(sf.nextLine());
}
假设您使用的是 Java.
Assuming you're using Java.
看看 http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html
这篇关于如何在while循环中填充数组列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文