Using ArrayList<int>

Hi!
I recently learned how to use ArrayLists and tried to use ArrayList<int> selection = new ArrayList<int>();
but the int does not work. Any ideas why?

Here is the code if this is an isolated issue which I doubt

//dynamic selection
ArrayList<int> selection = new ArrayList<int>();
//ArrayList<int> sel = new ArrayList<int>();
int w = 20, h = 20, clrs = 2;
float scl;
int grid[][] = new int[w][h];

void setup() {
  size(600,600);
  scl = width/w;
  for(int i = 0; i < w; i++) {
    for(int j = 0; j < h; j++) {
      grid[i][j] = floor(random(clrs));
    }
  }
  colorMode(HSB);
}

void draw() {
  background(0);
  for(int i = 0; i < w; i++) {
    for(int j = 0; j < h; j++) {
      fill(map(grid[i][j],0,clrs,0,255),255,255);
      rect(i*scl,j*scl,scl,scl);
    }
  }
}

void beginSelection(int x, int y) {
  selection.add(x+y*w);
}

btw I am aware of the Intlist but it seems useless compared to ArrayList if it exists.

Use
ArrayList<Integer>();

1 Like

it turns out I just suck at spelling

int is a primitive data type and generics requires a class Integer is a wrapper class for int

1 Like

Just a little bonus - arrays of primitive values are objects so
ArrayList<int[]> list;
is also OK :smile:

1 Like

so this serves as a flexible 2D array?

btw how to empty an arrayList?

I currently use

for(int i = 0; i < array.size(); i++) {
   array.remove(i)
}

In Java a 2D array is an array of 1D arrays so is fairly flexible anyway in that the 1D array doesn’t have to be the same length.

To empty an list you use
list.clear()

1 Like
for(int i = 0; i < list.size(); i++) {
   list.remove(i)
}

would be less confusing in this code

My current code is but it doesn’t spread into the bottom & left direction. Where did I go wrong?

THE CODE IS THE PREVIOUS VERSION (the only thing I changed was the doStuff() loop running more optimised (decreased the number of times calling the function))

//dynamic selection
ArrayList<Integer> selection = new ArrayList<Integer>();
//ArrayList<int> sel = new ArrayList<int>();
int w = 20, h = 20, clrs = 2;
float scl;
int grid[][] = new int[w][h];

void setup() {
  size(600,600);
  scl = width/w;
  for(int i = 0; i < w; i++) {
    for(int j = 0; j < h; j++) {
      grid[i][j] = floor(random(clrs));
    }
  }
  colorMode(HSB);
}

void draw() {
  background(0);
  for(int i = 0; i < w; i++) {
    for(int j = 0; j < h; j++) {
      fill(map(grid[i][j],0,clrs,0,255),255,255);
      rect(i*scl,j*scl,scl,scl);
      fill(255);
      if(selection.contains(i+j*w)) {
        rect((i+0.25)*scl, (j+0.25)*scl,scl/2,scl/2); 
      }
    }
  }
  //println(selection);
}

void mousePressed() {
  selection.clear();
  beginSelection( floor(map(mouseX,0,width,0,w)), floor(map(mouseY,0,height,0,h)));
}

void beginSelection(int x, int y) {
  selection.add(x+y*w);
  for(int i = 0; i < w*h; i++) {
    doStuff();
  }
  println(selection);
}

int doStuff() {
  int selected = 0;
  for(int i = 0; i < selection.size(); i++) {
    int gx = selection.get(i) % w, gy = floor((selection.get(i) - gx)/w);
    if(gx > 0) {
      if(grid[gx][gy] == grid[gx-1][gy] && !selection.contains( (gx-1)+gy*w) ) {
        selection.add((gx-1)+gy*w);
        selected++;
      }
    } else if(gx < w-1) {
      if(grid[gx][gy] == grid[gx+1][gy] && !selection.contains( (gx+1)+gy*w) ) {
        selection.add((gx+1)+gy*w);
        selected++;
      }
    }
    if(gy > 0) {
      if(grid[gx][gy] == grid[gx][gy-1] && !selection.contains( (gx)+(gy-1)*w)) {
        selection.add((gx)+(gy-1)*w);
        selected++;
      }
    } else if(gy < h-1) {
      if(grid[gx][gy] == grid[gx][gy+1] && !selection.contains( (gx)+(gy+1)*w)) {
        selection.add((gx)+(gy+1)*w);
        selected++;
      }
    }
  }
  
  return(selected);
}

this is an example

fixed the code to be better

//dynamic selection
ArrayList<Integer> selection = new ArrayList<Integer>();
int w = 20, h = 20, clrs = 2;
float scl;
int grid[][] = new int[w][h];

void setup() {
  size(600,600);
  scl = width/w;
  for(int i = 0; i < w; i++) {
    for(int j = 0; j < h; j++) {
      grid[i][j] = floor(random(clrs));
    }
  }
  colorMode(HSB);
}

void draw() {
  background(0);
  for(int i = 0; i < w; i++) {
    for(int j = 0; j < h; j++) {
      fill(map(grid[i][j],0,clrs,0,255),255,255);
      rect(i*scl,j*scl,scl,scl);
      fill(255);
      if(selection.contains(i+j*w)) {
        rect((i+0.25)*scl, (j+0.25)*scl,scl/2,scl/2); 
      }
    }
  }
  //println(selection);
}

void mousePressed() {
  selection.clear();
  beginSelection( floor(map(mouseX,0,width,0,w)), floor(map(mouseY,0,height,0,h)));
}

void beginSelection(int x, int y) {
  selection.add(x+y*w);
  while(doStuff() > 0) {
    
  }
  println(selection);
}

int doStuff() {
  int selected = 0;
  for(int i = 0; i < selection.size(); i++) {
    int gx = selection.get(i) % w, gy = floor((selection.get(i) - gx)/w);
    if(gx > 0) {
      if(grid[gx][gy] == grid[gx-1][gy] && !selection.contains( (gx-1)+gy*w) ) {
        selection.add((gx-1)+gy*w);
        selected++;
      }
    } else if(gx < w-1) {
      if(grid[gx][gy] == grid[gx+1][gy] && !selection.contains( (gx+1)+gy*w) ) {
        selection.add((gx+1)+gy*w);
        selected++;
      }
    }
    if(gy > 0) {
      if(grid[gx][gy] == grid[gx][gy-1] && !selection.contains( (gx)+(gy-1)*w)) {
        selection.add((gx)+(gy-1)*w);
        selected++;
      }
    } else if(gy < h-1) {
      if(grid[gx][gy] == grid[gx][gy+1] && !selection.contains( (gx)+(gy+1)*w)) {
        selection.add((gx)+(gy+1)*w);
        selected++;
      }
    }
  }
  
  return(selected);
}

an even better version (reduced the length of the code by 2 lines : P )

//dynamic selection
ArrayList<Integer> selection = new ArrayList<Integer>();
int w = 20, h = 20, clrs = 2;
float scl;
int grid[][] = new int[w][h];

void setup() {
  size(600,600);
  scl = width/w;
  for(int i = 0; i < w; i++) {
    for(int j = 0; j < h; j++) {
      grid[i][j] = floor(random(clrs));
    }
  }
  colorMode(HSB);
}

void draw() {
  background(0);
  for(int i = 0; i < w; i++) {
    for(int j = 0; j < h; j++) {
      fill(map(grid[i][j],0,clrs,0,255),255,255);
      rect(i*scl,j*scl,scl,scl);
      fill(255);
      if(selection.contains(i+j*w)) {
        rect((i+0.25)*scl, (j+0.25)*scl,scl/2,scl/2); 
      }
    }
  }
  //println(selection);
}

void mousePressed() {
  selection.clear();
  beginSelection( floor(map(mouseX,0,width,0,w)), floor(map(mouseY,0,height,0,h)));
}

void beginSelection(int x, int y) {
  selection.add(x+y*w);
  while(doStuff() > 0) {
    
  }
  println(selection);
}

int doStuff() {
  int selected = 0;
  for(int i = 0; i < selection.size(); i++) {
    int gx = selection.get(i) % w, gy = floor((selection.get(i) - gx)/w);
    if(gx > 0) {
      if(grid[gx][gy] == grid[gx-1][gy] && !selection.contains( (gx-1)+gy*w) ) {
        selection.add((gx-1)+gy*w);
        selected++;
      }
    } else if(gx < w-1) {
      if(grid[gx][gy] == grid[gx+1][gy] && !selection.contains( (gx+1)+gy*w) ) {
        selection.add((gx+1)+gy*w);
        selected++;
      }
    }
    if(gy > 0) {
      if(grid[gx][gy] == grid[gx][gy-1] && !selection.contains( (gx)+(gy-1)*w)) {
        selection.add((gx)+(gy-1)*w);
        selected++;
      }
    } else if(gy < h-1) {
      if(grid[gx][gy] == grid[gx][gy+1] && !selection.contains( (gx)+(gy+1)*w)) {
        selection.add((gx)+(gy+1)*w);
        selected++;
      }
    }
  }
  
  return(selected);
}

The code is confusing because I am not sure what you expect the sketch to do.

it is supposed to find all the squares of the same color (or value) that are sharing an edge with it

edit: and put them into an ArrayList (in form of a number (x + y*w))

i just realised why.

the if else thing is wrong

works :slight_smile:

finished code:

//dynamic selection
ArrayList<Integer> selection = new ArrayList<Integer>();
int w = 20, h = 20, clrs = 2;
float scl;
int grid[][] = new int[w][h];

void setup() {
  size(600,600);
  scl = width/w;
  for(int i = 0; i < w; i++) {
    for(int j = 0; j < h; j++) {
      grid[i][j] = floor(random(clrs));
    }
  }
  colorMode(HSB);
}

void draw() {
  background(0);
  for(int i = 0; i < w; i++) {
    for(int j = 0; j < h; j++) {
      fill(map(grid[i][j],0,clrs,0,255),255,255);
      rect(i*scl,j*scl,scl,scl);
      fill(255);
      if(selection.contains(i+j*w)) {
        rect((i+0.25)*scl, (j+0.25)*scl,scl/2,scl/2); 
      }
    }
  }
  //println(selection);
}

void mousePressed() {
  selection.clear();
  beginSelection( floor(map(mouseX,0,width,0,w)), floor(map(mouseY,0,height,0,h)));
}

void beginSelection(int x, int y) {
  selection.add(x+y*w);
  while(doStuff() > 0);
  println(selection);
}

int doStuff() {
  int selected = 0;
  for(int i = 0; i < selection.size(); i++) {
    int gx = selection.get(i) % w, gy = floor((selection.get(i) - gx)/w);
    if(gx > 0) {
      if(grid[gx][gy] == grid[gx-1][gy] && !selection.contains( (gx-1)+gy*w) ) {
        selection.add((gx-1)+gy*w);
        selected++;
      }
    }
    if(gx < w-1) {
      if(grid[gx][gy] == grid[gx+1][gy] && !selection.contains( (gx+1)+gy*w) ) {
        selection.add((gx+1)+gy*w);
        selected++;
      }
    }
    if(gy > 0) {
      if(grid[gx][gy] == grid[gx][gy-1] && !selection.contains( (gx)+(gy-1)*w)) {
        selection.add((gx)+(gy-1)*w);
        selected++;
      }
    } 
    if(gy < h-1) {
      if(grid[gx][gy] == grid[gx][gy+1] && !selection.contains( (gx)+(gy+1)*w)) {
        selection.add((gx)+(gy+1)*w);
        selected++;
      }
    }
  }
  
  return(selected);
}

OK so you fixed it well done :smiley:

to be honest it would have taken me at least an hour if I wasn’t thinking of how to explain the program and randomly opening it on the } else if() { part, while thinking of a random thing

well trouble shooting is usually 60% luck