[英]JAVA fill a temporary Arraylist with objects and then add it into a ArrayList which consists of arrayLists
I iterate through an Array which consists of objects. 我遍历一个由对象组成的数组。 I want to find certain parts of this array in which the same specific objects follow eachother. 我想找到此数组的某些部分,其中相同的特定对象彼此跟随。
[a,b,c,#,#,#,h,g,a,#,#,s,#.h] --> I want to find #,#,# and #,# (# is the specific object) [a,b,c,#,#,#,h,g,a,#,#,s,#。h]->我要查找#,#,#和#,#(#是特定的宾语)
I already figured out how to do this: If I find a '#' I will add this object to a temporary ArrayList. 我已经弄清楚了如何执行此操作:如果找到一个“#”,则将该对象添加到临时ArrayList中。 If the next object is a '#' too, I will add it aswell, otherwise I clear the tmplist, because it is a single '#'. 如果下一个对象也是“#”,我也将其添加,否则我将清除tmplist,因为它是单个“#”。 If the next object is not a # but the tmplist is bigger than 1 i want to add the tmplist to a 2d ArrayList (An ArrayList which consists of ArrayLists) and clear the tmplist so I can find other parts. 如果下一个对象不是#,但是tmplist大于1,我想将tmplist添加到2d ArrayList(由ArrayList组成的ArrayList)中,并清除tmplist,以便我可以找到其他部分。
Here is my problem: If i do this the the 2d arraylist does not consist of deepcopys of the templists--> The 2d arraylist consist of empty lists, because I clear the tmplist after every found "pattern". 这是我的问题:如果执行此操作,则2d arraylist不会包含临时列表的深拷贝-> 2d arraylist包含空列表,因为我会在找到每个“模式”之后清除tmplist。 How can I fix this? 我怎样才能解决这个问题?
Some code which might explain it better: 一些代码可能会更好地解释它:
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)
if your 2d ArrayList is : result
如果您的2d ArrayList为: result
do 做
result.add(new ArrayList<>(tmpList));
By doing this, you are not adding the tmpList
itself but a new list with the values of tmpList
. 这样,您将不会添加tmpList
本身,而是添加一个具有tmpList
值的新列表。 So even If you do tmpList.clear()
it will not affect the arraylist in your result
. 因此,即使您执行tmpList.clear()
,也不会影响result
的arraylist。
不是执行tmplist.clear()
而是执行tmplist = new ArrayList<>()
因此您每次都使用不同的List实例。
我认为最简单的解决方案是不清除列表,而是在将其添加到列表列表之后,将新的ArrayList引用分配给tmplist。
Ok..I know you must have sorted this out by now using the suggestions.... but I just thought I would make program to do this ... Here is what my solution looks like, not the best way to do it perhaps, but should work ... 好吧..我知道您现在必须已经使用建议进行了整理。...但是我只是想我要编写程序来执行此操作……这就是我的解决方案的样子,而不是最好的解决方法,但应该可以...
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()));
}
}
}
Hope that helps :) 希望有帮助:)
Ouput : :
[#, #] [#,#]
[#, #, #] [#,#,#]
[3, 3] [3,3]
Your code, modified according to @heenenee 's answer: 您的代码根据@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.