【发布时间】:2013-01-08 20:48:28
【问题描述】:
如何将此 ArrayList 的值转换为数组?所以它看起来像,
String[] textfile = ... ;
这些值是字符串(文本文件中的单词),并且有超过 1000 个单词。在这种情况下,我不能执行 words.add("") 1000 次。那么我怎样才能把这个列表放入一个数组中呢?
public static void main(String[]args) throws IOException
{
Scanner scan = new Scanner(System.in);
String stringSearch = scan.nextLine();
List<String> words = new ArrayList<String>(); //convert to array
BufferedReader reader = new BufferedReader(new FileReader("File1.txt"));
String line;
while ((line = reader.readLine()) != null) {
words.add(line);
}
【问题讨论】:
-
为什么你不能做
words.add(...)1000 次? -
很难理解你问题的本质。调用 words.add() 1000 次有什么问题?
-
我首先要问为什么你想要一个字符串数组?除非您有一个只接受数组的 API,否则您可以使用
List代替数组。特别是考虑到ArrayList由数组支持。
标签: java arrays algorithm arraylist