Arraylist not updated using arrays in object

Hi
I have been working quite a lot with Processing the last months. Now I am trying to add ArrayList of objects but I get such a strange result as though the List is not updated correctly.
I guess I am making a stupid formal mistake but cannot find out what!

Shortly: I defining the corners of a square and a triangle and want to store them in the Object class Kloss as arrays. There are two objects in the list (simplified as much as I could). Element 0 contains the Kloss object defining a square and element 1 defining a triangle.
When I, in the end of the code, list the content of the objcts the data for the first object (square) shows correct for the variable (NumberofCorners) but not for the array (rcorner) pointing out the corners of the shape.
Kylle

ArrayList<Kloss> klosslist = new ArrayList<Kloss>();
void setup(){
  size(400,400);
  PVector[] rcorner=new PVector[4]; 
  float B=100;
  // shape of square
  rcorner[0]=new  PVector(width/2,height/2);
  rcorner[1]=new PVector(width/2+B,height/2);
  rcorner[2]=new PVector(width/2+B,height/2-B);
  rcorner[3]=new PVector(width/2,height/2-B);
  klosslist.add(new Kloss(4,rcorner));
 
//TRIANGLE
  rcorner[0]=new  PVector(width/2+200,height/2);
  rcorner[1]=new PVector(width/2+200+B,height/2);
  rcorner[2]=new PVector(width/2+200+B/2,height/2-B*sqrt(3)/2);
  klosslist.add(new Kloss(3,rcorner));

//Test
  println("list size ",klosslist.size());
  Kloss k = klosslist.get(0);
  println("shape 0-Square:",k.NumberofCorners,k.rcorner[0],k.rcorner[1],k.rcorner[2]);
  k = klosslist.get(1);
  println("shape 1-Triangle:",k.NumberofCorners,k.rcorner[0],k.rcorner[1],k.rcorner[2]); 
}
   
  class Kloss {
  PVector[]rcorner =new PVector[4];  
  int NumberofCorners;

    Kloss(int NumberofCornersT,PVector[] rcornerT ) {
    NumberofCorners=NumberofCornersT;
    rcorner=rcornerT;
    }
  }  //end off class

Both of your 2 instances of class Kloss are sharing the same PVector[] array rcorner.

Basically when you’re creating new PVector instances for your triangle shape and assigning them to rcorner, you’re also replacing the 1s you did for your square shape.

As a quick fix, you can create another PVector[] array and assign it to rcorner before starting your triangle shape:
rcorner = new PVector[3];

Or take a look at the changes I did to your sketch below:

// https://Discourse.processing.org/t/
// arraylist-not-updated-using-arrays-in-object/35200/2

// by Kylle (2022-Feb-16)
// mod by GoToLoop

import java.util.List;
final List<Kloss> klossar = new ArrayList<Kloss>();

static final int B = 100;

void setup() {
  size(600, 400);
  noLoop();

  stroke(#FFFF00); // yellow border
  strokeWeight(2.5);

  final int cx = width >> 1, cy = height >> 1; // center coordinates

  klossar.add(new Kloss(#0000FF, new PVector[] { // blue square
    new PVector(cx, cy), 
    new PVector(cx + B, cy), 
    new PVector(cx + B, cy - B), 
    new PVector(cx, cy - B)
    }));

  klossar.add(new Kloss(#FF0000, new PVector[] { // red triangle
    new PVector(cx + B*2, cy), 
    new PVector(cx + B*3, cy), 
    new PVector(cx + B*2 + B/2, cy - B/2 * sqrt(3))
    }));

  for (final Kloss kloss : klossar)  println(kloss.corners);
  println("List size: " + klossar.size());
}

void draw() {
  background(0);
  for (final Kloss kloss : klossar)  kloss.display();
}

class Kloss {
  PVector[] corners;  
  color c;

  Kloss(final color colour, final PVector... vecs) {
    corners = vecs;
    c = colour;
  }

  void display() {
    fill(c);

    beginShape();
    for (final PVector v : corners)  vertex(v.x, v.y);
    endShape(CLOSE);
  }
}

Hi

Thanks a lot!
I thought that the locations were saved within each Object of the array in a new and different place. Obviously there are just pointers to the first assigned location and since the data there are changed it will incorrect for the first object.

Thanks
Kylle