清除 Java 中的 ArrayList

huangapple go评论159阅读模式
英文:

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&lt;ArrayList&lt;String&gt;&gt; arr = new ArrayList&lt;ArrayList&lt;String&gt;&gt;();
		ArrayList&lt;String&gt; x = new ArrayList&lt;String&gt;();
		
	for(int i = 0 ; i &lt; arrCol ; i++) {	
		
	    
        x.addAll(Arrays.asList(b.readLine().split(&quot;\\s&quot;))); 
        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&lt;List&lt;String&gt;&gt; arr = new ArrayList&lt;&gt;();
  • No need to use an auxiliary List. Just do it as follows:
    for(int i = 0 ; i &lt; arrCol ; i++) {         
        arr.add(Arrays.asList(b.readLine().split(&quot;\\s&quot;)));
    }
  • If you still want to create an explicit ArrayList&lt;&gt; implementation, then pass the output of Arrays.asList as an argument. No need to use add or addAll.
    for(int i = 0 ; i &lt; arrCol ; i++) {         
        arr.add(new ArrayList&lt;&gt;(Arrays.asList(b.readLine().split(&quot;\\s&quot;))));
    }

  • 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 &lt; arrCol ; i++) { 
    ArrayList&lt;String&gt; x = new ArrayList&lt;String&gt;();
    ...

... 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 &lt; arrCol ; i++) { 
    
    ArrayList&lt;String&gt; x = new ArrayList&lt;&gt;();
    x.addAll(Arrays.asList(b.readLine().split(&quot;\\s&quot;))); 
    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&lt;String&gt; x = new ArrayList&lt;String&gt;();

huangapple
  • 本文由 发表于 2020年8月24日 05:15:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/63552069.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定