Checking if all elements in an array are the same

So I have been trying to get a program to work, the point of the program is to display every image in an arraylist in a random order (taking the mouse as input to go to the next image) and then reset and repeat to do this I made this piece of code.

void draw() {

if (clear == true) {

for (int i = cardCollection.size() - 1; i >= 0; i--) {

  cardCollection.set(i, false);

clear = false;


if (cardCollection.get(randomVal) == false && pressResolve == true) {

image(cards.get(randomVal), width/2, height/2, width/2, height/2);
pressResolve = false;
cardCollection.set(randomVal, true);

for (Boolean b : cardCollection) {

  if (!b.equals(cardCollection.get(0))) {

    clear = false;
  } else {

    clear = true;

} else {

randomVal = int(random(0, cards.size()));


void mousePressed() {

randomVal = int(random(0, cards.size()));
pressResolve = true;

However it does not seem to be going through all the images but rather resetting even when only one image has been displayed.


i am not sure if that helps, but I notice this:

step 1: you set all values in the index list of images to false, then clear to false to prevent of resetting it every frame.

step 2: if the mouse has been pressed, the next part gets the image fitting to your random index. then you set its index value to true (= it has already been shown)

step 3: next you iterate through the index list of booleans. if a value is not the same the value in the first one (…get(0)) you set clear to false. if their is an item that is not the same, set clear to true - effectively resetting the whole list.

step 4: if the selected item is already on true, make a new random value (find another image)

I believe there is something wrong with step 3

First of all, you always compare with the boolean at index 0.
If by chance the first randomValue is 0, the first image is displayed and set too true.
From that on, ALL other images will not fill the condition, so no more images can be selected. and the list will be reset.

Second you iterate through the whole list, setting clear again and again. So basically you ask if the value of the first item (0) is different only from the last items value.
Which can never happen (both are on false) unless you hit the first or the last item by chance.
If you do not hit that, the list is reset to all false again.

So only if the first randomValue found is the LAST item, it value will be != the first items value.

1st random Value = 0:
display the first image -> set its index value to true
boolean of image 0 equals itself -> clear = true -> all images are on false again

1st random Value = last image:
display the last image -> set its index value to true
boolean of last image does not equal image 0 -> chance to get another image by clicking

1st random Value = any image between first and last:
display that image -> set its index value to true
boolean of the last image equals first image -> clear = true -> all images are on false again

I think you should change your structure a little:

  1. all index values are false
  2. choose a random number
  3. if its value is true (literally -> if (cards.getValue(randomVal)) choose another randomValue or do nothing but wait for another click
  4. -> else, if its value is false -> display this image, set its value to true
  5. if there are no “false” values in the array (-> hasValue()) you are finished -> maybe now reset the list, so you can start again.

• i don’t think you need the clear variable. you could make a method to clear the list and call it whenever you need it. it does not have to be inside draw. same goes for your main loop, it could be another method that is called from within mousePressed.

• you could also have an IntList that has all the the indices of all your images. (0,1,2,3,4,5…)
whenever you generate a random value, you remove that item from the IntList leaving (0,1,2,4,5)
make the next randomValue up to the intList size.

so, first time the IntList hast the values (0,1,2) at the indices (0,1,2)

randomValue = (int(0,IntList.length) ->randomValue is 1 -> get its value (1) -> store it in a variable -> remove the entry

the list looks like this: values (0,2) at indices (0,1)
(int(0,IntList.length) -> … -> remove entry

hope this helps a little :slight_smile:

1 Like

To display something in random order, some people also just shuffle the list and then loop through it with just i++, because the elements are shuffled.



  • Write your own routine as you are doing.
    This is always rewarding and you can learn a lot along the way.

  • Use an existing function.
    A search for ArrayList in the Processing references and in the related items you will find a function that may be of interest:

  • Anything I have not listed.
    I am always in awe of the unique solutions that people find.


1 Like

haha, a very wise solution!

1 Like

Agreed, this was (and still is) standard practice as far as I know.