开发者

How to avoid Java.util.IllegalStateException in the following code?

开发者 https://www.devze.com 2023-04-10 11:06 出处:网络
I have a List of integer\'s with duplicate values in it. What I need to do is find the duplicate integers, add their value and then add the result to the list by remov开发者_运维技巧ing the duplicates

I have a List of integer's with duplicate values in it. What I need to do is find the duplicate integers, add their value and then add the result to the list by remov开发者_运维技巧ing the duplicates found. Here is what I am doing:

List<Integer> list1 = new ArrayList<Integer>();
    list1.add(2);
    list1.add(5);
    list1.add(3);
    list1.add(5);
    list1.add(4);

    List<Integer> list2 = new ArrayList<Integer>();
    Iterator<Integer> it = list1.iterator();
    while (it.hasNext()) {
        Integer int1 = it.next();
        if (list2.isEmpty()) {
            list2.add(int1);
            it.remove();
        } else {
            ListIterator<Integer> it2 = list2.listIterator();
            while (it2.hasNext()) {
                Integer int2 = it2.next(); 
                if (int2 != int1) {
                    it2.add(int1);
                    it.remove();// I get exception here

                } else {                        
                    it2.remove();
                    it.remove();
                    Integer newint = int1 + int2;
                    it2.add(newint);
                }                   
            }
        }
    }       
    for(Integer in : list2){
        System.out.println(in);
    }

Output should look like

2

10

3

4

Thanks for your time.


As the other posters have said, you can't remove while iterating. Even though there are 'tricks', messing with a collection while iterating is a surefire way to get weird runtime bugs.

Anyway, you are working way too hard on the problem.

Here's a quick and dirty solution with a fraction of the code:

private List<Integer> sumAndUniqDuplicates(List<Integer> list) {
    LinkedHashMap<Integer, Integer> lookup = new LinkedHashMap<Integer, Integer>();
    for (Integer value : list) {
        Integer prevValue = lookup.get(value);
        prevValue = (prevValue == null) ? 0 : prevValue;
        lookup.put(value, prevValue + value);
    }
    return new ArrayList<Integer>(lookup.values());
}


If you are allowed to use a Map you could do something simple like this (incoming pseudocode):

create empty Map m
for each Integer x in list1 do
    if m does not contain key x 
        m.put(x, x)
    else
        m.put(x, m.get(x) + x)
    endif
done

Your result are the values of m (which is a Collection).

Edit: You said you have LatLng instead of Integers - I don't know LatLng but after a quick google I'd take a shot at the following, assuming that you want to "add" up your LatLng points:

create empty Map<LatLng, LatLng> m
for each LatLng x in list1 do
    if not m.containsKey(x) 
        m.put(x, x)
    else
        m.put(x, LatLng.newInstance(m.get(x).getLatitude() + x.getLatitude(),
                                    m.get(x).getLongitude() + x.getLongitude()))
    endif
done

The only problem I can see here is that this m.containsKey(x) depends on the correct implementation of equals, which I'm not sure after reading this


It is because you remove the same element twice. First time in if(list2.isEmpty()) (because list2 is empty in the beginning and immediately after that in the else body.


From the documentation for the remove method:

Removes from the underlying collection the last element returned by the iterator (optional operation). This method can be called only once per call to next.


You cannot remove the current element twice. You need to rethink your logic.

0

精彩评论

暂无评论...
验证码 换一张
取 消