您能告诉我以下代码有什么问题:
Scanner input = new Scanner(System.in);
Vector <Vector <String>> allValues = new Vector <Vector <String>>();
Vector <String> currentTestValues = new Vector <String>();
int tests = input.nextInt();
for (int i = 0; i < tests; i++){
int deposits = input.nextInt();
for (int j = 0; j < deposits; j++){
String s = input.nextLine();
currentTestValues.add(s);
}
allValues.add(currentTestValues);
currentTestValues.clear();
}
for (Vector <String> v : allValues){
for (String s : v){
System.out.println(s);
}
}
似乎在input.nextLine();
如何解决?
答案 0 :(得分:0)
已使用input.nextLine()
和currentTestValues.clear();
。这将从列表中删除您的值,因此从vector中删除它。删除它。
Scanner input = new Scanner(System.in);
Vector <Vector <String>> allValues = new Vector <Vector <String>>();
Vector <String> currentTestValues = new Vector <String>();
int tests = Integer.parseInt(input.nextLine());
for (int i = 0; i < tests; i++){
int deposits = Integer.parseInt(input.nextLine());
for (int j = 0; j < deposits; j++){
String s = input.nextLine();
currentTestValues.add(s);
}
allValues.add(currentTestValues);
}
for (Vector <String> v : allValues){
for (String s : v){
System.out.println(s);
}
}
}