# Bug with ArrayList

**URL:** <https://discourse.processing.org/t/bug-with-arraylist/5949>\
**Category:** Coding Questions\
**Created:** [November 26, 2018, 12:07pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949 "2018-11-26T12:07:53Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![bitCrasher](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bitcrasher/32/408_2.png) [@bitCrasher](https://discourse.processing.org/u/bitCrasher)\
**Post date:** [November 26, 2018, 12:07pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949/1 "2018-11-26T12:07:53Z")

</div>

hi guys,  
I’m having a bug that I just can not solve, I think it’s a very stupid thing.  
in practice I would like this code to generate a map of points, which save in an arreyList, and then that there are 4 lines that intersect the point. So far, so good. the problem arises when I move the lines from one point to another interpolating the previous point with the next one. in part it works, but not for each loop cycle.

```auto
ArrayList<Point> points = new ArrayList<Point>();
Line lines;
ArrayList<PVector> momPos = new ArrayList<PVector>();
int i=1;
int k=0;

void setup() {
  size(1280, 720, P2D);
  for (int i=0; i<20; i++) {
    points.add(new Point());
  }
  for (float j=0.; j<=1.; j+=.01) {
    momPos.add(new PVector(lerp(points.get(i-1).pos.x, points.get(i).pos.x, j), lerp(points.get(i-1).pos.y, points.get(i).pos.y, j)));
  }
}

void draw() {
  background(0);
  noStroke();
  fill(255);
  for (Point p : points) {
    ellipse(p.pos.x, p.pos.y, 8, 8);
  }

  stroke(255, 0, 0);
  if (i>=points.size()-1) i=1;

  if (k>momPos.size()-1) {
    while (momPos.size()>0) {
      for (int j=0; j<momPos.size(); j++) {
        momPos.remove(j);
      }
    }
    for (float j=0.; j<=1.; j+=.01) {
      momPos.add(new PVector(lerp(points.get(i-1).pos.x, points.get(i).pos.x, j), lerp(points.get(i-1).pos.y, points.get(i).pos.y, j)));
    }
    println("hei");
    k=0;
    points.remove(i-1);
    i++;
  }
  lines = new Line(momPos.get(k));
  lines.lineRender();
  k++;
}

```

```auto
class Line {
  PVector pos;
  Line(PVector pos) {
    this.pos=pos;
  }
  
  void lineRender(){
    line(0, pos.y, pos.x, pos.y);
    line(pos.x, 0, pos.x, pos.y);
    line(pos.x, pos.y, width, pos.y);
    line(pos.x, pos.y, pos.x, height);
  }
}

```

```auto
class Point {
  PVector pos;
  Point() {
    pos=new PVector(random(width), random(height));
  }
}

```

Thanks for your help!!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 26, 2018, 3:25pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949/2 "2018-11-26T15:25:13Z")

</div>

- use PVector for a random point OK
- use a ArrayList for a number of this points good,  
because you want add and remove

– the use of class did not come into my mind for this points or target-line drawing  
as it is not shorter or smarter… but, why not.

i just did not understand what the second ArrayList “momPos” is for?  
using the same points?

meanwhile not sure what you want to do anyhow, i try my own  
test, possibly take a look

> **Auto Shooter**
>
> ```auto
> // https://discourse.processing.org/t/bug-with-arraylist/5949?u=kll
> // make a arraylist of pvector points
> // calc shortest distance to the next target, aim, fire, remove the point....
> 
> int idt, pmax = 10;
> ArrayList<PVector> points = new ArrayList<PVector>();
> PVector target = new PVector(0, 0);
> boolean theend = false, shoot = false;
> 
> int k = 0; //firing circles
> 
> void setup() {
> size( 500, 500);
> for (int i = 0; i < pmax; i++) points.add(new PVector(random(-width/2, width/2), random(-height/2, height/2)));
> for ( PVector p : points ) println(p);
> }
> 
> void draw_lines() {
> stroke(200, 0, 0);
> line(target.x, -height/2, target.x, height/2); 
> line(-width/2, target.y, width/2, target.y);
> }
> 
> void draw_points() {
> for (int i = 0; i < points.size(); i++) { 
> if ( i == idt ) stroke(200, 0, 0); 
> else noStroke();
> fill(0, 200, 0);
> ellipse(points.get(i).x, points.get(i).y, 8, 8);
> }
> }
> 
> int get_nearest() {
> int idx = -1;
> float tdist = width;
> for (int i = 0; i < points.size(); i++) {
> float thisdist = dist(points.get(i).x, points.get(i).y, target.x, target.y);
> if ( thisdist < tdist ) { 
> tdist = thisdist; 
> idx = i;
> }
> }
> return idx;
> }
> 
> void aim() {
> if ( idt > -1 ) {
> PVector waytotarget = points.get(idt).copy();
> waytotarget.sub(target);
> if ( waytotarget.mag() > 0.5 ) {
> waytotarget.setMag(waytotarget.mag()*0.01); // calc faster to short step in that direction
> target = target.add(waytotarget); // change target
> } else { 
> if ( !shoot ) println("shoot at "+idt); // only print 
> shoot = true;
> points.remove(idt);
> }
> } else { 
> println(" i win ! "); 
> theend = true;
> }
> }
> 
> void fire() {
> if ( shoot) {
> fill(200, 100, 0);
> ellipse(target.x, target.y, 2*k, 2*k);
> k++;
> if ( k >= 30 ) {  
> shoot = false; 
> k = 0;
> }
> }
> }
> 
> void show_end() {
> fill(0, 200, 0);
> rect(-50, -25, 100, 40);
> fill(200, 0, 0);
> text(" I WIN ! ", -20, 0);
> }
> 
> void draw() {
> background(200, 200, 0);
> translate(width/2, height/2);
> if ( !theend ) {
> idt = get_nearest();
> draw_lines();
> draw_points();
> if ( !shoot ) aim(); // not walk while firing
> fire();
> } else show_end();
> }
> 
> ```

if i am OFF, just ignore it.

---

<div class="post-metadata">

**Author:** ![bitCrasher](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bitcrasher/32/408_2.png) [@bitCrasher](https://discourse.processing.org/u/bitCrasher)\
**Post date:** [November 26, 2018, 3:46pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949/3 "2018-11-26T15:46:29Z")

</div>

momPos is to generate the all the position between two point using lerp to interpolate.

I would like the cross to move from point a to point b, then from point b to point c and so on

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 26, 2018, 3:49pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949/4 "2018-11-26T15:49:12Z")

</div>

> [@bitCrasher](#):
>
> I would like the cross to move from point a to point b, then from point b to point c and so on

ok, that i do, just with one arrraylist and some vector math even with speed calc…  
did you check it?

---

<div class="post-metadata">

**Author:** ![bitCrasher](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bitcrasher/32/408_2.png) [@bitCrasher](https://discourse.processing.org/u/bitCrasher)\
**Post date:** [November 26, 2018, 3:53pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949/5 "2018-11-26T15:53:26Z")

</div>

yes…maybe i will change that…but the problem is in the index i suppose…btw  
if i dont remove elements from the list(comment line 40) it works like i want!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 26, 2018, 4:01pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949/6 "2018-11-26T16:01:34Z")

</div>

but if you remove a point, you need to rebuild the other array…  
in the JAVA doc i see a .clear() does that work?

i avoided all this just by finding the index of the nearest point and walk to it

---

<div class="post-metadata">

**Author:** ![bitCrasher](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bitcrasher/32/408_2.png) [@bitCrasher](https://discourse.processing.org/u/bitCrasher)\
**Post date:** [November 26, 2018, 4:03pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949/7 "2018-11-26T16:03:39Z")

</div>

mmh i rebuilt the momPos array every time k is \> of momPos array

---

<div class="post-metadata">

**Author:** ![bitCrasher](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bitcrasher/32/408_2.png) [@bitCrasher](https://discourse.processing.org/u/bitCrasher)\
**Post date:** [November 26, 2018, 4:05pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949/8 "2018-11-26T16:05:11Z")

</div>

yes…clear work…but the line restart from the wrong point…i continue to think that is a problem about index!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 26, 2018, 4:14pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949/9 "2018-11-26T16:14:21Z")

</div>

definitely, never the point where the cross is removed, always a other one

---

<div class="post-metadata">

**Author:** ![bitCrasher](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bitcrasher/32/408_2.png) [@bitCrasher](https://discourse.processing.org/u/bitCrasher)\
**Post date:** [November 26, 2018, 4:25pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949/10 "2018-11-26T16:25:22Z")

</div>

how can i fix it? i’m going crazy lol

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 26, 2018, 4:28pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949/11 "2018-11-26T16:28:11Z")

</div>

i am not able to follow your code, like the “k” thing…  
but i see that you first make a

> momPos.add

( using what points??? )  
and later a

> points.remove(i-1);

sounds wrong,  
after remove need a  
rebuild\_momPos();

---

<div class="post-metadata">

**Author:** ![bitCrasher](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bitcrasher/32/408_2.png) [@bitCrasher](https://discourse.processing.org/u/bitCrasher)\
**Post date:** [November 26, 2018, 4:31pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949/12 "2018-11-26T16:31:03Z")

</div>

i add the points generated in the class a in the void setup.  
k is used to iterate and stop momPos array

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 26, 2018, 4:38pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949/13 "2018-11-26T16:38:07Z")

</div>

again, you delete the wrong point ( what i see )  
and the cursor jump to different place and start walking to a new target.

and in code you do

> momPos.add(…points.get(i-1)…points.get(i)

and then

> points.remove(i-1);

??\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
pls. can you confirm that how it is happening in my version  
is actually that what you want? because from your code i not see that.  
and possibly you plan later a real operator shooting…  
how i know?

---

<div class="post-metadata">

**Author:** ![bitCrasher](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bitcrasher/32/408_2.png) [@bitCrasher](https://discourse.processing.org/u/bitCrasher)\
**Post date:** [November 26, 2018, 4:50pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949/14 "2018-11-26T16:50:04Z")

</div>

yes it jump to the wrong point…if you delete  
points.remove(i-1);  
it work exactly how i want…

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 26, 2018, 4:56pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949/15 "2018-11-26T16:56:39Z")

</div>

> [@bitCrasher](#):
>
> if you delete  
> points.remove(i-1);

pls. move that line UP.

---

<div class="post-metadata">

**Author:** ![bitCrasher](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bitcrasher/32/408_2.png) [@bitCrasher](https://discourse.processing.org/u/bitCrasher)\
**Post date:** [November 26, 2018, 4:58pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949/16 "2018-11-26T16:58:16Z")

</div>

up where? out the if?

---

<div class="post-metadata">

**Author:** ![bitCrasher](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bitcrasher/32/408_2.png) [@bitCrasher](https://discourse.processing.org/u/bitCrasher)\
**Post date:** [November 26, 2018, 5:00pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949/17 "2018-11-26T17:00:03Z")

</div>

nope…but if i put it up…it doesn’t remove the right point!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 26, 2018, 5:03pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949/18 "2018-11-26T17:03:18Z")

</div>

yes, and it never did.  
so move it up and delete the right one

```auto
  if (k>momPos.size()-1) {
    while (momPos.size()>0) {
      for (int j=0; j<momPos.size(); j++) {
        momPos.remove(j);
      }
    }

    println("hei");
    k=0;
    points.remove(i);
    i++;

    for (float j=0.; j<=1.; j+=.01) {
      momPos.add(new PVector(lerp(points.get(i-1).pos.x, points.get(i).pos.x, j), lerp(points.get(i-1).pos.y, points.get(i).pos.y, j)));
    }
  }

```

---

<div class="post-metadata">

**Author:** ![bitCrasher](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bitcrasher/32/408_2.png) [@bitCrasher](https://discourse.processing.org/u/bitCrasher)\
**Post date:** [November 26, 2018, 5:28pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949/19 "2018-11-26T17:28:10Z")

</div>

not work! it starts ever from a wrong point

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 26, 2018, 5:29pm UTC](https://discourse.processing.org/t/bug-with-arraylist/5949/20 "2018-11-26T17:29:29Z")

</div>

i not know about start, but here it removes the right point

[Next page](https://discourse.processing.org/t/bug-with-arraylist/5949.md?page=2)
