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)
I already figured out how to do this: If I find a '#' I will add this object to a temporary ArrayList. If the next object is a '#' too, I will add it aswell, otherwise I clear the tmplist, because it is a single '#'. 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.
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". 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
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
. So even If you do tmpList.clear()
it will not affect the arraylist in your result
.
不是执行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]
Your code, modified according to @heenenee 's answer:
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.