【问题标题】:JAVA fill a temporary Arraylist with objects and then add it into a ArrayList which consists of arrayListsJAVA 用对象填充临时 Arraylist,然后将其添加到由 arrayLists 组成的 ArrayList
【发布时间】:2015-10-12 20:10:54
【问题描述】:

我遍历一个由对象组成的数组。我想找到这个数组的某些部分,其中相同的特定对象彼此跟随。

[a,b,c,#,#,#,h,g,a,#,#,s,#.h] --> 我要查找 #,#,# 和 #,# (#是具体的对象)

我已经想出了如何做到这一点:如果我找到一个“#”,我将把这个对象添加到一个临时的 ArrayList 中。如果下一个对象也是一个“#”,我也会添加它,否则我会清除 tmplist,因为它是一个单一的“#”。如果下一个对象不是 # 但 tmplist 大于 1,我想将 tmplist 添加到 2d ArrayList(由 ArrayList 组成的 ArrayList)并清除 tmplist,以便我可以找到其他部分。

这是我的问题:如果我这样做,则 2d arraylist 不包含 templists 的 deepcopys--> 2d arraylist 由空列表组成,因为我在每个找到的“模式”之后清除 tmplist。我该如何解决这个问题?

一些可以更好地解释它的代码:

List<Object> tmplist = new ArrayList<Object>();
   for (int i = 0; i<array.length(); i++) {
       if (array[i].equals(#)) {
          tmplist.add(array[i]);
              if (!array[i+1].equals(#) && tmplist.size() < 2){
                  tmplist.clear();
              } else if (!array[i+1].equals(#) && tmplist.size() > 1) {
                   pattern.add(tmplist);
                   tmplist.clear();
              } 
       }
   }
   //pattern is the 2d ArrayList (ArrayList which consists of ArrayLists)

【问题讨论】:

标签: java arrays list multidimensional-array arraylist


【解决方案1】:

您的代码,根据@heeneee 的回答修改:

List<Object> tmplist = new ArrayList<Object>();
for (int i = 0; i<array.length(); i++) {
    if (array[i].equals(#)) {
        tmplist.add(array[i]);
        if (!array[i+1].equals(#) && tmplist.size() < 2) {
            tmplist.clear();
        } else if (!array[i+1].equals(#) && tmplist.size() > 1) {
            pattern.add(tmplist);
            tmplist = new ArrayList<Object>(); // Easy changes here
        }
    }
}
//pattern is the 2d ArrayList (ArrayList which consists of ArrayLists)

【讨论】:

    【解决方案2】:

    好的..我知道您现在必须使用建议来解决这个问题....但我只是想我会编写程序来执行此操作...这是我的解决方案的样子,不是最好的方法也许可以,但应该可以...

    import java.util.ArrayList;
    
    
    import javax.swing.text.rtf.RTFEditorKit;
    
    public class ArrTest {
    public static void main(String[] args) {
        String[] text = new String[] { "a","#","#","b", "c", "#", "#","#", "b", "3", "3" };
        Map<Integer, List<String>> retMap = new HashMap<Integer, List<String>>();
        List<String> repeatString;
        int count = 0;
        for (int i = 1; i < text.length; i++) {
            if(text[i].equals(text[i-1])){
                repeatString = new ArrayList<String>();
                repeatString.add(text[i]);
                for(int j=i;j<text.length;j++){
                    if(text[j].equals(text[i])){
                        repeatString.add(text[j]);
                    } else {
                        break;
                    }
                }
                retMap.put(count, repeatString);
                count++;
                i++;
            }
        }
    
        Iterator<Integer>iter = retMap.keySet().iterator();
        while(iter.hasNext()){
            System.out.println(retMap.get(iter.next()));
        }
    }
    }
    

    希望有帮助:)

    输出:

    [#, #]
    [#, #, #]
    [3, 3]

    【讨论】:

      【解决方案3】:

      如果您的 2d ArrayList 是:result

       result.add(new ArrayList<>(tmpList));
      

      通过这样做,您不会添加tmpList 本身,而是添加具有tmpList 值的新列表。因此,即使您执行tmpList.clear(),也不会影响您的result 中的数组列表。

      【讨论】:

      • @Peter111 我很高兴它有帮助:)
      【解决方案4】:

      我认为最简单的解决方案是不清除列表,而是将新引用分配给 tmplist,然后将其添加到列表列表中。

      【讨论】:

        【解决方案5】:

        不要使用tmplist.clear() 而是使用tmplist = new ArrayList&lt;&gt;(),这样您每次都使用不同的 List 实例。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-09
          • 2020-11-08
          • 2013-10-30
          • 2013-10-29
          • 2017-12-12
          相关资源
          最近更新 更多