英文:
clear arraylist in java
问题
public class main6 {
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader b = new BufferedReader(in);
int arrCol = scan.nextInt();
ArrayList<ArrayList<String>> arr = new ArrayList<ArrayList<String>>();
ArrayList<String> x = new ArrayList<String>();
for (int i = 0; i < arrCol; i++) {
x.addAll(Arrays.asList(b.readLine().split("\\s")));
arr.add(i, x);
x.clear();
}
System.out.println(Arrays.deepToString(arr.toArray()));
}
}
英文:
i try to make a 2D arrayList when i accept data and store it into a single arraylist then added it to the 2d arraylist so i clear the single array list in every loop and save the new row the problem in clearing data it clear all bast row and just save the newest one enter code here
public class main6 {
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
InputStreamReader in = new InputStreamReader(System.in);
BufferedReader b = new BufferedReader(in);
int arrCol= scan.nextInt();
ArrayList<ArrayList<String>> arr = new ArrayList<ArrayList<String>>();
ArrayList<String> x = new ArrayList<String>();
for(int i = 0 ; i < arrCol ; i++) {
x.addAll(Arrays.asList(b.readLine().split("\\s")));
arr.add(i,x);
x.clear();
}
System.out.println(Arrays.deepToString(arr.toArray()));
}
}
</details>
# 答案1
**得分**: 1
一些注意事项:
- 最佳实践是在接口类型上赋值,而不是在实现类型上赋值。同时,你对“2D” ArrayList的声明是错误的。所以应该这样写:
```java
List<List<String>> arr = new ArrayList<>();
- 不需要使用辅助的List。只需要按照以下方式进行操作:
for (int i = 0; i < arrCol; i++) {
arr.add(Arrays.asList(b.readLine().split("\\s")));
}
- 如果你仍然想创建一个明确的
ArrayList<>
实现,那么将Arrays.asList
的输出作为参数传递即可,不需要使用add或addAll。
for (int i = 0; i < arrCol; i++) {
arr.add(new ArrayList<>(Arrays.asList(b.readLine().split("\\s"))));
}
- 不需要转换为数组并进行深度字符串转换。List不需要这样做。
System.out.println(arr);
注意:第一个注意事项有一个例外情况。有时,List
实现在方法方面具有更多的功能,而这些功能不在List
接口中。在这种情况下,如果你需要那些功能,就需要赋值给实现类型,而不是接口类型。对于List
接口,我不记得需要这样做过。
英文:
A couple pf things.
- Best practice is to assign to the interface type in lieu of the implementation. And you're declaring your "2D" ArrayList incorrectly. So
List<List<String>> arr = new ArrayList<>();
- No need to use an auxiliary List. Just do it as follows:
for(int i = 0 ; i < arrCol ; i++) {
arr.add(Arrays.asList(b.readLine().split("\\s")));
}
- If you still want to create an explicit
ArrayList<>
implementation, then pass the output ofArrays.asList
as an argument. No need to use add or addAll.
for(int i = 0 ; i < arrCol ; i++) {
arr.add(new ArrayList<>(Arrays.asList(b.readLine().split("\\s"))));
}
- No need to convert to an array and do deep string conversion. Lists don't need it.
System.out.println(arr);
Note: One exception to to the first bullet. Sometimes, a List
implementation has more functionality in terms of methods
that are not in the List interface
. In that case, if you want that functionality, then you need to assign to the implementation type rather than the interface type. With List
interfaces, I cannot recall having to do that.
答案2
得分: 0
在for循环中创建列表,这样每次添加操作都会有新的实例:
for (int i = 0; i < arrCol; i++) {
ArrayList<String> x = new ArrayList<String>();
...
}
另外,之后不要清空它。
英文:
Create the list in the for loop, so you have new instance for every addition:
for(int i = 0 ; i < arrCol ; i++) {
ArrayList<String> x = new ArrayList<String>();
...
... also, don't clear it afterwards...
答案3
得分: 0
将for循环更新以实例化数组列表:
for (int i = 0; i < arrCol; i++) {
ArrayList<String> x = new ArrayList<>();
x.addAll(Arrays.asList(b.readLine().split("\\s")));
arr.add(i, x);
}
这样,数组列表将在每次循环中重新创建,无需清除它。
然后,您可以在for循环之前的方法体中删除数组列表的实例化:
ArrayList<String> x = new ArrayList<String>();
英文:
Update the for loop to instantiate the array list:
for(int i = 0 ; i < arrCol ; i++) {
ArrayList<String> x = new ArrayList<>();
x.addAll(Arrays.asList(b.readLine().split("\\s")));
arr.add(i,x);
}
This way, the array list will be recreated for each loop and there is no need to clear it.
You can then remove the instantiation of the arraylist in the method body before the for-loop:
ArrayList<String> x = new ArrayList<String>();
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论