繁体   English   中英

JAVA使用对象填充临时Arraylist,然后将其添加到由arrayLists组成的ArrayList中

[英]JAVA fill a temporary Arraylist with objects and then add it into a ArrayList which consists of arrayLists

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

[a,b,c,#,#,#,h,g,a,#,#,s,#。h]->我要查找#,#,#和#,#(#是特定的宾语)

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

这是我的问题:如果执行此操作,则2d arraylist不会包含临时列表的深拷贝-> 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)

如果您的2d ArrayList为: result

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

这样,您将不会添加tmpList本身,而是添加一个具有tmpList值的新列表。 因此,即使您执行tmpList.clear() ,也不会影响result的arraylist。

不是执行tmplist.clear()而是执行tmplist = new ArrayList<>()因此您每次都使用不同的List实例。

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

好吧..我知道您现在必须已经使用建议进行了整理。...但是我只是想我要编写程序来执行此操作……这就是我的解决方案的样子,而不是最好的解决方法,但应该可以...

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]

您的代码根据@heenenee的答案进行了修改:

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM