# ArrayList Problems / get() & remove()

**URL:** https://discourse.processing.org/t/arraylist-problems-get-remove/1150
**Category:** Coding Questions
**Created:** [June 21, 2018, 1:18pm UTC](https://discourse.processing.org/t/arraylist-problems-get-remove/1150 "2018-06-21T13:18:55Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![RC75018](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rc75018/32/578_2.png) [@RC75018](https://discourse.processing.org/u/RC75018)
#### Post date: [June 21, 2018, 1:18pm UTC](https://discourse.processing.org/t/arraylist-problems-get-remove/1150/1 "2018-06-21T13:18:55Z")

</div>

Hi everyone,

I am a newbie in processing, at my early stages of learning and playing with this amazing language. I am trying to create a kind of “space invader” program from scratch.

I don’t want to get inspired by the real one, just trying to make my own version to challenge myself.

I have divided my reflexion into smaller manageable parts and now i am stucked. I am currently working on the “bullets part”. The idea is to create two shots (controlled by mouseX & mouseY) every time that mousePressed is activated.

I am using an ArrayList, and i can’t understand why but it says that the functions get() & remove() do not exist.

Can someone help me on this one please?

Thank you,

Rémy

```auto

ArrayList<Bullet> bullets;

void setup(){
  size(400,400);
  smooth();
  
  bullets = new ArrayList<Bullet>();
  
  bullets.add(new Bullet(mouseX,mouseY));
  
}

void display(){
  background(255);
  
  for (int i = bullets.size()-1; i >= 0; i--) { 
  Bullet bullets = bullets.get(i);
  bullets.move();
  bullets.display();
  
  if (bullets.finished()){
    bullets.remove(i);
  }
 }
}

void mousePressed(){
  bullets.add(new Bullet(mouseX,mouseY));
}

class Bullet{
  
  float bulletX1, bulletY1;
  float bulletX2, bulletY2;
  int r;
  int bulletSpeed;
  
  Bullet(float _x, float _y){
    
    bulletX1 = _x - 10;
    bulletY1 = _y;
    bulletX2 = _x +10;
    bulletY2 = _y;
    r = 6;
    bulletSpeed = 1;
  
  }
  
  void move(){
    bulletY1 -= bulletSpeed;
    bulletY2 -= bulletSpeed;
  }
  
    boolean finished() {
    if ((bulletY1<0) || (bulletY2<0)) {
      return true;
    } else {
      return false;
    }
  }
  
  void display(){
    fill(255,0,0);
    noStroke();
    ellipse (bulletX1,bulletY1,r,r);
    ellipse (bulletX2,bulletY2,r,r);  
  }
}

```

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [June 21, 2018, 1:39pm UTC](https://discourse.processing.org/t/arraylist-problems-get-remove/1150/2 "2018-06-21T13:39:05Z")

</div>

Don’t use the same name for your variable

- `ArrayList<Bullet> bullets;`  
and
- `Bullet bullets = bullets.get(i);`

Say (for example) :

- `Bullet b = bullets.get(i);`

By the way for ArrayLists you can do this instead :

```auto
for(Bullet b : bullets){
    //Your code : b.move()...
}

```

---

<div class="post-metadata">

### Author: ![RC75018](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rc75018/32/578_2.png) [@RC75018](https://discourse.processing.org/u/RC75018)
#### Post date: [June 21, 2018, 1:47pm UTC](https://discourse.processing.org/t/arraylist-problems-get-remove/1150/3 "2018-06-21T13:47:04Z")

</div>

Hi @josephh,

Thank you for your message. Sorry for asking but why do i need to change the name? I really want to make sure that i understand well the logic behind.

Also, i just changed the variable name but the screen is grey and this is still not working.  
I changed the “size” position cause i had a message error when it was packed in setup()

Best,

Rémy

```auto
ArrayList<Bullet> bullets;

void settings(){
   size(400,400);
}

void setup(){

  smooth();
  
  bullets = new ArrayList<Bullet>();
  
  bullets.add(new Bullet(mouseX,mouseY));
  
}

void display(){
  background(255);
  
  for (int i = bullets.size()-1; i >= 0; i--) { 
  Bullet b = bullets.get(i);
  b.move();
  b.display();
  
  if (b.finished()){
    bullets.remove(i);
  }
 }
}

void mousePressed(){
  bullets.add(new Bullet(mouseX,mouseY));
}

class Bullet{
  
  float bulletX1, bulletY1;
  float bulletX2, bulletY2;
  int r;
  int bulletSpeed;
  
  Bullet(float _x, float _y){
    
    bulletX1 = _x - 10;
    bulletY1 = _y;
    bulletX2 = _x +10;
    bulletY2 = _y;
    r = 6;
    bulletSpeed = 1;
  
  }
  
  void move(){
    bulletY1 -= bulletSpeed;
    bulletY2 -= bulletSpeed;
  }
  
    boolean finished() {
    if ((bulletY1<0) || (bulletY2<0)) {
      return true;
    } else {
      return false;
    }
  }
  
  
  void display(){
    fill(255,0,0);
    noStroke();
    ellipse (bulletX1,bulletY1,r,r);
    ellipse (bulletX2,bulletY2,r,r);  
  }
  

}

```

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [June 21, 2018, 2:05pm UTC](https://discourse.processing.org/t/arraylist-problems-get-remove/1150/4 "2018-06-21T14:05:41Z")

</div>

> [@RC75018](#):
>
> Sorry for asking but why do i need to change the name?

Because two variables mustn’t have the same name.

> [@RC75018](#):
>
> Also, i just changed the variable name but the screen is grey and this is still not working.

The reason if that you have to call the function :

```auto
void draw(){

}

```

instead of :

```auto
void display(){

}

```

---

<div class="post-metadata">

### Author: ![RC75018](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rc75018/32/578_2.png) [@RC75018](https://discourse.processing.org/u/RC75018)
#### Post date: [June 21, 2018, 2:08pm UTC](https://discourse.processing.org/t/arraylist-problems-get-remove/1150/5 "2018-06-21T14:08:22Z")

</div>

Awesome. Of course! I didn’t saw it.

Thank you @josephh for the advices.

Best,

Rémy

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [June 24, 2018, 6:04pm UTC](https://discourse.processing.org/t/arraylist-problems-get-remove/1150/6 "2018-06-24T18:04:05Z")

</div>

> [@RC75018](#):
>
> I am using an ArrayList, and i can’t understand why but it says that the functions get() & remove() do not exist.

Regarding why that specific error: As @josephh pointed out, you first name your ArrayList `bullets` and then, in your for loop, you give a single bullet the same name – `bullets`. So, your error that `get` does not exist is because `bullets` is now of class Bullet, not of class ArrayList, and class Bullet doesn’t have a `get()` method.

Two variables in different scope may have the same name – or you may redefine a variable name to point to a different object. However, redefining names can often lead to confusion.

1. “When I say Big Red, I mean my dog.”
2. “When I say Big Red, I mean my car.”
3. “Please feed Big Red.”  
“…how do you feed a car?” ( method feed does not exist for object of class Car )

---

<div class="post-metadata">

### Author: ![RC75018](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rc75018/32/578_2.png) [@RC75018](https://discourse.processing.org/u/RC75018)
#### Post date: [June 25, 2018, 10:41am UTC](https://discourse.processing.org/t/arraylist-problems-get-remove/1150/7 "2018-06-25T10:41:48Z")

</div>

Hi @jeremydouglass,

Thank you for your explanation, it makes totally sens!

By the way, i am pretty impressed by the help and reactivity of the community.

You guys are the best!

Rémy

---

<div class="post-metadata">

### Author: ![RC75018](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/rc75018/32/578_2.png) [@RC75018](https://discourse.processing.org/u/RC75018)
#### Post date: [July 10, 2018, 10:05am UTC](https://discourse.processing.org/t/arraylist-problems-get-remove/1150/8 "2018-07-10T10:05:48Z")

</div>

Hi Everyone,

So my code is progressing well. I am looking for a way to erase the aliens when bullets touch them.  
I am a bit lost regarding the boolean variable using the distance between the bullets and the round body of the aliens.

I want to kill the aliens when a bullet hits them, erase them from the screen and from the list.

A solution for it?

Thank you,

Rémy

```auto

Alien[]aliens = new Alien[1000];
SpaceShip spaceship;
ArrayList<Bullet> bullets;
Timer timer;

int totalAliens = 0;

void setup(){
  
  size(600,400);
  smooth();
  frameRate(60);
  
  timer = new Timer(1000);
  timer.start();
  
  spaceship = new SpaceShip(10);
  
    
  bullets = new ArrayList<Bullet>();
  bullets.add(new Bullet(mouseX,mouseY));
  
}

void draw(){
  
  background(255);
  
    if(timer.isFinished()){
       aliens[totalAliens] = new Alien();
       totalAliens++;
       if (totalAliens >= aliens.length){
         totalAliens = 0;
       }
      timer.start();
    }
  
  for (int i = bullets.size()-1; i >= 0; i--) { 
  Bullet b = bullets.get(i);
  b.move();
  b.display();
  
  if (b.finished()){
    bullets.remove(i);
  }
 }
  
  
  for (int i = 0; i < totalAliens ; i++){
  aliens[i].move();
  aliens[i].display();
  }
  
  spaceship.move(mouseX, mouseY);
  spaceship.display();

}

void mousePressed(){
  bullets.add(new Bullet(mouseX,mouseY));
}

class Timer{

  int savedTime;
  int totalTime;

  Timer(int _tempTotalTime){
    totalTime = _tempTotalTime; 
  }

  void start(){
    savedTime = millis();
  }

  boolean isFinished(){
    int passedTime = millis() - savedTime;
    if(passedTime > totalTime){
      return true;
    } else {
      return false;
    }
  }
}

class Alien {
  
  float x;
  float y;
  float diam;
  float speed;
  
  Alien(){
    x = random(width);
    y = random(-100,-50);
    diam = random(20,40);
    speed = diam*0.1;
  }
  
  
  void move(){
    y += speed;
  }
  
  
  void display(){
    
    stroke(0);
    strokeWeight(diam*0.1);

    ellipseMode(CENTER);
    noFill();
    arc(x, y + diam*0.5, 2*diam, diam, -PI, 0);
  
    arc (x-diam,y+diam*0.75,diam*0.5,diam*0.5,-PI,0);
    arc (x+diam,y+diam*0.75,diam*0.5,diam*0.5,-PI,0);
  
    fill(200);
    ellipse(x,y,diam,diam);
  }
  
  boolean reachedBottom(){
    if (y > height + diam*4){
      return true;
    } else {
      return false;
    }
  }
  
}

class Bullet{
  
  float bulletX1, bulletY1;
  float bulletX2, bulletY2;
  int r;
  int bulletSpeed;
  
  Bullet(float _x, float _y){
    
    bulletX1 = _x - 20;
    bulletY1 = _y - 15;
    bulletX2 = _x + 20;
    bulletY2 = _y - 15;
    r = 6;
    bulletSpeed = 2;
  
  }
  
  void move(){
    bulletY1 -= bulletSpeed;
    bulletY2 -= bulletSpeed;
  }
  
    boolean finished() {
    if ((bulletY1<0) || (bulletY2<0)) {
      return true;
    } else {
      return false;
    }
  }
  
  void display(){
    fill(255,0,0);
    noStroke();
    ellipse (bulletX1,bulletY1,r,r);
    ellipse (bulletX2,bulletY2,r,r);  
  }
  
   boolean intersect(Alien d){
   float distance = dist(bulletX1,bulletY1,d.x,d.y);
  if (distance < r + d.diam){
    return true;
  } else {
    return false;
  }
}
  

}

class SpaceShip{
  
float x;
float y;
int shipRatio;

 SpaceShip(int tempShipRatio){
   x = width/2;
   y = height/2;
   shipRatio = tempShipRatio;
 }
 
 void move (float tempX, float tempY){
   x = tempX;
   y = tempY + shipRatio*2;;
   
 }
 
 void display(){
  stroke(0);
  strokeWeight (3);
  fill(210);
  
  line (x - shipRatio*2, y - shipRatio, x - shipRatio*2, y - shipRatio*4);
  line (x + shipRatio*2, y - shipRatio, x + shipRatio*2, y - shipRatio*4);
  
  triangle (x - shipRatio*3, y - shipRatio, x, y - shipRatio*4, x + shipRatio*3, y-shipRatio);
  
  }
}

```

---

<div class="post-metadata">

### Author: ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)
#### Post date: [July 10, 2018, 10:20am UTC](https://discourse.processing.org/t/arraylist-problems-get-remove/1150/9 "2018-07-10T10:20:06Z")

</div>

It’s going to be easy to remove the bullets that hit from your ArrayList because an ArrayList is specifically designed to allow you to remove elements from it.

Removing a single Alien from an Array of Aliens is not going to be as easy.

So… why are you using an Array of Aliens, instead of an ArrayList of them? You don’t actually need space for 1000 aliens at a time. Just a few that are on the screen.

Anyway, I’m getting ahead of myself. You want to do collisions. You need to check - for each alien - if any bullet is hitting it.

Thus you will first need to loop over every alien.

```
for ( each alien ){

```

Then you will need to loop ove each bullet.

```
  for( each bullet ) {

```

Then you need to see if those collide.

```
if( alien.collides( bullet ) ){

```

If so, you remove that alien and that bullet.

* * *

When is a round bullet colliding with a round alien? Basically, when are two circles intersecting? What can you say about the distance between their centers in relationship to the sum of their radii?

* * *

You might see this thread I posted recently:

> [@Problem With Collision](https://discourse.processing.org/t/problem-with-collision/1539/9):
>
> Anyway. Collisions. Since the thing that we really care about colliding with is enemies, let’s loop over every enemy. if we collide that enemy with the player, the player dies. Then let’s loop over all the bullets. If that enemy collides with that bullet, both that enemy and that bullet die. … What does it mean for an object to die? Well, we can add an alive boolean to each of our objects. And then only draw them if they are alive. And of course only alive things can cause collisions. class P…
