【问题标题】:why only adding last element arraylist on new arraylist [closed]为什么只在新的arraylist上添加最后一个元素arraylist [关闭]
【发布时间】:2015-05-30 23:00:46
【问题描述】:

有目标结果: [[a],[a,b],[a,b,c]]

但是当我运行我的代码时,结果是: [[a,b,c],[a,b,c],[a,b,c]]

这是源代码:

public class Try {


    ArrayList<String> al=new ArrayList<String>();
    List<List<String>> hm=new ArrayList<List<String>>();


    public void addal2(){
        for(int j=0; j<3;j++){
            al.clear();
            for(int i=0; i<1;i++){
                if(j==0){al.add("a");}
                else if(j==1){al.add("a");al.add("b");}
                else if(j==2){al.add("a");al.add("b");al.add("c");}
            }
            hm.add(al);
        }
        System.out.println("hm final"+hm);
    }

    public static void main(String[] args) {
        // TODO code application logic here
        Try c = new Try();
        c.addal2();
    }

}

请告诉我为什么结果是这样的 HM 只是添加了最后一个元素?

【问题讨论】:

  • 请在您的问题中更具体一些,stackOverflow 并不是用来发布半途而废的家庭作业并让我们修复它的地方。
  • 您添加了 3 次相同的列表并更改了其内容。不要在循环开始时清除它,而是创建一个新列表。
  • @khelwood 我想我可以在 j 的每个循环中获取每个 Arraylist al 并一次添加到 List hm,谢谢

标签: java for-loop arraylist duplicates element


【解决方案1】:

最后一个元素没有被添加到 HM,您正在向 HM 添加对同一列表的引用。

迭代 1:HM = ["a"]

迭代 2:HM = [["a", "b"], ["a","b"]]

迭代 3:HM = [["a", "b", "c"], ["a", "b", "c"], ["a", "b", "c"] ]

这很容易解决,无需清除列表,只需重新实例化即可。

public static void main(String[] args) throws Exception {
    List<List<String>> hm = new ArrayList();

    for(int j=0; j<3;j++){
        List<String> al = new ArrayList();
        for(int i=0; i<1;i++){
            if(j==0){al.add("a");}
            else if(j==1){al.add("a");al.add("b");}
            else if(j==2){al.add("a");al.add("b");al.add("c");}
        }
        hm.add(al);
    }
    System.out.println("hm final"+hm);
}

结果:

hm final[[a], [a, b], [a, b, c]]

更新

至于清理代码以使其更简单,您可以尝试以下方法:

public static void main(String[] args) throws Exception {
    List<List<String>> hm = new ArrayList();
    String alphabet = "abcdefghijklmnopqrstuvwxyz";

    // Change the 3 to any number from 1 - 26
    for (int i = 0; i < 3; i++) {
        List<String> al = new ArrayList();
        for (int j = 0; j <= i; j++) {
            al.add(String.valueOf(alphabet.charAt(j)));
        }
        hm.add(al);
    }
    System.out.println(hm);
}

结果:

[[a], [a, b], [a, b, c]]

【讨论】:

    【解决方案2】:

    我发现您的代码无法真正修复,我会这样做:

    public class Main {
      private static List<List<String>> addal2(final String... abc) {
        final List<List<String>> result = new ArrayList<>();
        for (final String x : abc) {
            if (result.isEmpty()) {
                result.add(Arrays.asList(x));
                continue;
            }
            final List<String> lastList = result.get(result.size() - 1);
            final List<String> newLastList = new ArrayList<>();
            newLastList.addAll(lastList);
            newLastList.add(x);
            result.add(newLastList);
        }
        return result;
      }
    
      public static void main(String[] args) {
        final List<List<String>> result = addal2("a", "b", "c");
        System.out.println(result); // Prints "[[a], [a, b], [a, b, c]]"
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      • 2012-09-11
      • 2016-08-15
      • 1970-01-01
      相关资源
      最近更新 更多