# Using ArrayList\<int\>

**URL:** <https://discourse.processing.org/t/using-arraylist-int/26094>\
**Category:** Beginners\
**Created:** [December 9, 2020, 6:47pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094 "2020-12-09T18:47:40Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 9, 2020, 6:47pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094/1 "2020-12-09T18:47:40Z")

</div>

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

```auto
//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);
}

```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 9, 2020, 7:05pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094/2 "2020-12-09T19:05:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [December 9, 2020, 7:08pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094/3 "2020-12-09T19:08:22Z")

</div>

> [@CodeMasterX](#):
>
> ArrayList();

Use  
`ArrayList<Integer>();`

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 9, 2020, 7:09pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094/4 "2020-12-09T19:09:36Z")

</div>

it turns out I just suck at spelling

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [December 9, 2020, 7:10pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094/5 "2020-12-09T19:10:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [December 9, 2020, 7:12pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094/6 "2020-12-09T19:12:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 9, 2020, 7:20pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094/7 "2020-12-09T19:20:48Z")

</div>

> [@quark](#):
>
> ArrayList\<int\> list;

so this serves as a flexible 2D array?

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 9, 2020, 7:41pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094/8 "2020-12-09T19:41:48Z")

</div>

btw how to empty an arrayList?

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 9, 2020, 7:43pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094/9 "2020-12-09T19:43:42Z")

</div>

I currently use

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

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [December 9, 2020, 7:43pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094/10 "2020-12-09T19:43:45Z")

</div>

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()`

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [December 9, 2020, 7:45pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094/11 "2020-12-09T19:45:25Z")

</div>

> [@CodeMasterX](#):
>
> ```auto
> for(int i = 0; i < array.size(); i++) {
> array.remove(i)
> }
> 
> ```

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

```

would be less confusing in this code

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 9, 2020, 7:47pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094/12 "2020-12-09T19:47:17Z")

</div>

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))

```auto
//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

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/9a847e40b86c199f8d0ea55ef2cac3f53303d711.png)

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 9, 2020, 7:49pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094/13 "2020-12-09T19:49:04Z")

</div>

fixed the code to be better

```auto
//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);
}

```

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 9, 2020, 7:52pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094/14 "2020-12-09T19:52:41Z")

</div>

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

```auto
//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);
}

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [December 9, 2020, 7:54pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094/15 "2020-12-09T19:54:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 9, 2020, 7:54pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094/16 "2020-12-09T19:54:53Z")

</div>

> [@CodeMasterX](#):
>
> `//dynamic selection`

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))

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 9, 2020, 7:56pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094/17 "2020-12-09T19:56:48Z")

</div>

i just realised why.

the if else thing is wrong

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 9, 2020, 7:57pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094/18 "2020-12-09T19:57:21Z")

</div>

works 🙂

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/3d6312527603dabeb5363c56df53da964e1c0456.png)

finished code:

```auto
//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);
}

```

---

<div class="post-metadata">

**Author:** ![quark](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/quark/32/26_2.png) [@quark](https://discourse.processing.org/u/quark)\
**Post date:** [December 9, 2020, 7:58pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094/19 "2020-12-09T19:58:01Z")

</div>

OK so you fixed it well done 😃

---

<div class="post-metadata">

**Author:** ![CodeMasterX](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/codemasterx/32/2942_2.png) [@CodeMasterX](https://discourse.processing.org/u/CodeMasterX)\
**Post date:** [December 9, 2020, 7:59pm UTC](https://discourse.processing.org/t/using-arraylist-int/26094/20 "2020-12-09T19:59:46Z")

</div>

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

[Next page](https://discourse.processing.org/t/using-arraylist-int/26094.md?page=2)
