列表未填满

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

The list is not filled

问题

你尝试将数据嵌套到一个列表中,然后将该列表添加到另一个列表中。挑战在于获得一组嵌套的数据列表。

String content1;
String content2;

ArrayList<ArrayList<String>> listData = new ArrayList<>();
ArrayList<String> listDataOne = new ArrayList<>();

for (int i = 0; i < 3; i++) {
    content1 = "one " + i;
    content2 = "two " + i;

    listDataOne.add(content1);
    listDataOne.add(content2);

    System.out.println(listDataOne);
    listData.add(listDataOne);
    System.out.println(listData); // [[one 2, two 2], [one 2, two 2], [one 2, two 2]]
    listDataOne.clear();
}
System.out.println(listData);  // [[], [], []]

但最后,你得到了空的嵌套列表。我做错了什么?

英文:

I am trying to nest data into a list, which I also add to another list. The challenge is to get a list of nested lists of data.

    String content1;
    String content2;

    ArrayList&lt;ArrayList&lt;String&gt;&gt; listData = new ArrayList&lt;&gt;();
    ArrayList&lt;String&gt; listDataOne = new ArrayList&lt;&gt;();

    for (int i = 0; i &lt; 3; i++) {
        content1 = &quot;one &quot; + i;
        content2 = &quot;two &quot; + i;

        listDataOne.add(content1);
        listDataOne.add(content2);

        System.out.println(listDataOne);
        listData.add(listDataOne);
        System.out.println(listData); // [[one 2, two 2], [one 2, two 2], [one 2, two 2]]
        listDataOne.clear();
    }
    System.out.println(listData);  // [[], [], []]

But in the end, I get empty nested lists. What am I doing wrong?

答案1

得分: 2

在你的for循环结束时,你清空了listDataOne,由于listData引用相同的列表,它也被清空了。你需要将

listDataOne.clear();

替换为

listDataOne = new ArrayList&lt;&gt;();

以保留数据。

英文:

At the end of your for loop you're clearing listDataOne and since listData has the same list reference, it gets cleared too. You need to replace

listDataOne.clear();

with

listDataOne = new ArrayList&lt;&gt;();

to preserve data.

答案2

得分: 0

这是翻译好的部分:

"这是因为 listData 存储的是对 listDataOne 的引用,而不是值。因此,当您清除引用时,将没有值,当您更新 listDataOne 时,所有对它的引用都将被更新。

    String content1;
    String content2;

    ArrayList<ArrayList<String>> listData = new ArrayList<>();
    ArrayList<String> listDataOne;

    for (int i = 0; i < 3; i++) {
        listDataOne = new ArrayList<>();
        content1 = "one " + i;
        content2 = "two " + i;

        listDataOne.add(content1);
        listDataOne.add(content2);

        System.out.println(listDataOne);
        listData.add(listDataOne);
        System.out.println(listData);  
  }
    System.out.println(listData);  

您还应该阅读此帖子。"

英文:

It's happing because listData is storing reference of listDataOne instead of value. So, when you clear the reference will have no value and when you update listDataOne all the reference to it will be updated.

    String content1;
    String content2;

    ArrayList&lt;ArrayList&lt;String&gt;&gt; listData = new ArrayList&lt;&gt;();
    ArrayList&lt;String&gt; listDataOne;

    for (int i = 0; i &lt; 3; i++) {
        listDataOne = new ArrayList&lt;&gt;();
        content1 = &quot;one &quot; + i;
        content2 = &quot;two &quot; + i;

        listDataOne.add(content1);
        listDataOne.add(content2);

        System.out.println(listDataOne);
        listData.add(listDataOne);
        System.out.println(listData);  
  }
    System.out.println(listData);  

You should also go through this this post.

huangapple
  • 本文由 发表于 2020年7月22日 21:59:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63035994.html
匿名

发表评论

匿名网友

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

确定