#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# ConcurrentModificationException Problem

edited February 2015

Hi there, as title says I'm having a ConcurrentModificationException error while running my sketch. I'm working on a single channel image, red, and I'm trying to scan every pixels and putting them in different arrays based on their colour. Here's the logic of what I want to do: - loop through every pixels; -if there's no arrays yet create one and append the colour of the first pixel found and its location; -then for the second pixel found loop through the existing arrays and compare its red component to their first element (the red component). If it finds an equal value its location gets added to that array, otherwise it creates another array containing red component an location. -And so on...

So let's say I have a 10x10 image and 50 pixels have red value == 255 and the other 50 == 100 my result should be: array1 = [255, pixelLocation,pixelLocation,pixelLocation, ......] array2 = [100, pixelLocation,pixelLocation,pixelLocation, ......]

I know what ConcurrentModificationException error means but I'm finding it hard to fix, it seems all logic to me.

Here's the sketch:

``````import java.util.List;

PImage img;
final List<IntList> arrayOfLists = new ArrayList();

void setup() {
size(600,600);
image(img,0,0);

for(int x = 0; x < width; x++){
for(int y = 0; y < height; y++){
int loc = x + y * width;

//GET R COMPONENT
float r = red(img.pixels[loc]);

if(arrayOfLists.size() == 0){         // IF ARRAY OF LISTS DOES NOT CONTAIN ANY LIST
IntList iR = new IntList(int(r)); // create a new list
iR.append(int(r));                //append pixel colour
iR.append(loc);                   //append pixel location
}//END OF IF
else{                                //LOOP THROUGH arrayOfLists
for(IntList list : arrayOfLists) {
if(int(r) == list.get(0)) {
list.append(loc);
} // END OF IF
else {
IntList iR = new IntList(int(r)); // create a new list
iR.append(int(r));                //append pixel colour as first element
iR.append(loc);                   //append pixel location
} // END of else
} // END of for lists in arrayOfLists
}//END OF ELSE

} // END of for X
} // END of for Y

println("NUM OF ARRAYS: ",arrayOfLists.size());
println("ITEMS IN FIRST ARRAY:", arrayOfLists.get(0));
}//END setup
``````
Tagged:

• Don't add things to the list while iterating on it.

• A possible way to do this is:

``````import java.util.List;

PImage img;
final List<IntList> arrayOfLists = new ArrayList();

void setup() {
size(600, 600);
image(img, 0, 0);

for (int x = 0; x < img.width; x++) {
for (int y = 0; y < img.height; y++) {
int loc = x + y * img.width;

//GET R COMPONENT
float r = red(img.pixels[loc]);
boolean found = false;

if (arrayOfLists.size() == 0) {         // IF ARRAY OF LISTS DOES NOT CONTAIN ANY LIST
IntList iR = new IntList(int(r)); // create a new list
iR.append(int(r));                //append pixel colour
iR.append(loc);                   //append pixel location
found = true;
}//END OF IF
else {                                //LOOP THROUGH arrayOfLists
for (IntList list : arrayOfLists) {
if (int(r) == list.get(0)) {
list.append(loc);
found = true;
break;
} // END OF IF
} // END of for lists in arrayOfLists
if (!found)
{
IntList iR = new IntList(int(r)); // create a new list
iR.append(int(r));                //append pixel colour as first element
iR.append(loc);                   //append pixel location
}
}//END OF ELSE
} // END of for X
} // END of for Y

println("NUM OF ARRAYS: ", arrayOfLists.size());
println("ITEMS IN FIRST ARRAY:", arrayOfLists.get(0));
}//END setup
``````

Notes:

• I rely on img' size, not on sketch size (even if I suppose they are the same on your sketch).
• I am not sure of the logic of my code, with regard of the result you want to achieve. This is more to show the idea: no need (perhaps?) to continue to explore the arrays if you found the pixel; if not found, add a new array.
• I reformatted the code with Ctrl+T in the PDE. It is slightly more readable.
• Thanks PhiLho, your code works and I'm getting the expected results :D I'm not able to accept your answer, probably because you left a comment.
{"ft_lang_label":"__label__en","ft_lang_prob":0.73827845,"math_prob":0.91181296,"size":2611,"snap":"2020-24-2020-29","text_gpt3_token_len":668,"char_repetition_ratio":0.12773302,"word_repetition_ratio":0.08851675,"special_character_ratio":0.27996936,"punctuation_ratio":0.17769375,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9885958,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-05-27T07:25:41Z\",\"WARC-Record-ID\":\"<urn:uuid:9421765d-2ead-47a7-b071-4312be232712>\",\"Content-Length\":\"47234\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:886b8005-bcad-41b2-b091-e1519fe821d6>\",\"WARC-Concurrent-To\":\"<urn:uuid:0024ea14-d49b-4002-990a-44a9e06fcee9>\",\"WARC-IP-Address\":\"45.33.79.91\",\"WARC-Target-URI\":\"https://forum.processing.org/two/discussion/9472/concurrentmodificationexception-problem\",\"WARC-Payload-Digest\":\"sha1:YZCKGFNGMJQ55MVFCDPZFIWFRMWZOECQ\",\"WARC-Block-Digest\":\"sha1:T3FL2M3RRBV4EEM73DGU46TS7ZPCF4ZQ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-24/CC-MAIN-2020-24_segments_1590347392141.7_warc_CC-MAIN-20200527044512-20200527074512-00488.warc.gz\"}"}