1

当我们处理多种情况时,每次都要接受新的字符串行,存储它们的最佳方式是什么?(类似于我们在编程竞赛中得到的东西)例如:假设有 t 个案例将作为用户输入给出。如果输入,则后面跟着 t 行。那么我们在这里使用数组吗?还是别的什么?如果我使用数组作为

Scanner sc=new Scanner(System.in);
String input=sc.next();

这种方式不会有用,因为输入变量只能使用一次,对吧?

4

1 回答 1

0

You'll want to use an ArrayList if you need to store the data. An ArrayList is just like an array, except that it grows dynamically as you add elements to it.

Define it with:

List<String> myList = new ArrayList<String>();

Then as the user enters something, write

myList.add(someStringVariable);
于 2013-07-14T22:05:18.167 回答