2

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)
Peter111
  • 803
  • 3
  • 11
  • 21

5 Answers5

4

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.

Karthik
  • 4,950
  • 6
  • 35
  • 65
2

Instead of doing tmplist.clear() do tmplist = new ArrayList<>() so you're working with a different List instance each time.

heenenee
  • 19,914
  • 1
  • 60
  • 86
1

I think easiest solution would be to not clear the list, but instead assign to tmplist new reference to ArrayList, after adding it to list of lists.

newfolder
  • 108
  • 2
  • 9
1

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]

digidude
  • 289
  • 1
  • 8
1

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)
Jason Sparc
  • 702
  • 2
  • 7
  • 29