【问题标题】:Duplicate entries for Array List数组列表的重复条目
【发布时间】:2018-07-28 13:54:36
【问题描述】:

我不想将新项目添加到现有列表中,而是想为新项目创建一个新列表。例如,

FINAL OUTPUT:[[case1, this is method A], [case2, this is method A]]

但是,我的代码输出是

FINAL OUTPUT:[[case1, this is method A, case2, this is method A], [case1, this is method A, case2, this is method A]]

我不太确定我哪里出错了。 任何帮助不胜感激!谢谢!

以下是我的代码。

   static List<List<String>> list = new ArrayList<>();

    static ArrayList<String> temp = new ArrayList<>();

    public static void main(String[] args) {
        for (int q = 1; q < 3; q++) {
            switch (q) {
            case 1:
                temp.add("case1");
                methodA();
                list.add(temp);
                break;

            case 2:
                temp.add("case2");
                methodA();
                list.add(temp);
                break;
            }
        }
        System.out.println("FINAL OUTPUT:" + list);
    }

    private static void methodA() {
        temp.add("this is method A");
    } 

【问题讨论】:

  • temp 最好在循环内声明。并发送至methodA(List&lt;String&gt; temp)。您遇到的问题是使用范围超出必要范围的常见后果。
  • 这种形式的循环绝对没有意义,见en.wikipedia.org/wiki/Loop-switch_sequence
  • @Clashsoft,不,不是。 OP要输入switch 2次
  • @Andrew Tobilko 循环和切换毫无意义,请参阅链接。第一次进入 switch 时 q 是 1,第二次是 2。他可以去掉循环头、switch、case 标签和 break 语句,代码会做和以前一样的事情,而且更容易了解。
  • @Clashsoft,if OP 想要 test 使用这个 sn-p 的所有可能(和可接受的)情况? (如果这不是这段代码的目的,那你是绝对正确的)。

标签: java arraylist multidimensional-array


【解决方案1】:

由于clear() 会影响已添加到最终结果中的列表(在上一次迭代中),因此您必须先复制 (1),然后再将其清除 (2)。

list.add(new ArrayList<>(temp));  // 1
temp.clear();                     // 2

让我们从 switch 中移出 3 行重复的行。

switch (q) {
    case 1:
        temp.add("case1");
        break;

    case 2:
        temp.add("case2");
        break;
}
methodA();
list.add(new ArrayList<>(temp));
temp.clear();

【讨论】:

    【解决方案2】:

    您必须在每个循环中清除临时列表或重新启用它。我个人更喜欢选项 2。

    ArrayList<String> temp = new ArrayList<>();   
    
    public static void main(String[] args) {
            for (int q = 1; q < 3; q++) {
    
                temp = new ArrayList<>();
    
                switch (q) {
                case 1:
                    temp.add("case1");
                    methodA();
                    list.add(temp);
                    break;
    
                case 2:
                    temp.add("case2");
                    methodA();
                    list.add(temp);
                    break;
                }
            }
    

    【讨论】:

      【解决方案3】:

      发生这种情况是因为您将完整的 Arraylist 添加到字符串列表中而不清除它。 您可以做的是在每个 case 语句中清除 arrayList temp

      for (int q = 1; q < 3; q++) {
              switch (q) {
              case 1:
                  temp = new ArrayList<>();
                  temp.add("case1");
                  methodA();
                  list.add(temp);
                  break;
      
              case 2:
                  temp = new ArrayList<>();
                  temp.add("case2");
                  methodA();
                  list.add(temp);
                  break;
              }
          }
      

      【讨论】:

      • 你最终会得到一个包含引用同一对象的元素的列表
      • @AndrewTobilko 说得对,评论错了。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 2015-06-10
      • 2012-12-06
      • 1970-01-01
      • 2014-02-27
      • 1970-01-01
      相关资源
      最近更新 更多