ArrayList with boolean type crashing on use of the .get function

I am trying to use an arraylist of booleans but when using the .get function on the array the program crashes, so I figured I should have iniatilized the values however when trying to do so it simply seems to skip over the loop and says the variable in the loop is not used. I’m trying to get something like below to work. I’m getting an indexOutOfBoundsExeption with the error saying the size is 0
ArrayList switches = new ArrayList(32);

void setup() {

size(200, 200);

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

boolean s = switches.get(i);

s = false;

}

if (switches.get(12)) {

ellipse(width/2, height/2, 50, 50);

}
}

void mousePressed() {

switches.set(12, true);
}

ArrayList switches = new ArrayList();  //!!!!!!!

void setup() {
  size(200, 200);

  println(switches.size());   //!!!!!!!!!!!!!!

  for (int i =32; i >= 0; i--) {
    switches.add (  false ) ;  //!!!!!!!!!!!
  }
}

/// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
void draw() {  /// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  if ( (boolean)switches.get(12)) {  //!!!!!!!!
    ellipse(width/2, height/2, 50, 50);
  }
}

void mousePressed() {
  switches.set(12, true);
}

Ah thanks for helping me out with the syntax, couldn’t find a lot of info on that

1 Like