Problem with ArrayList<Float>

I stumbled upon a problem a few times. It’s either this or error: index: 0 size: 0. Why is this happening. I am relatively new to this but I can’t do anything. It sometimes fixes on itself and at other times it just doesn’t.

ArrayList<Float> x = new ArrayList<Float>();
ArrayList<Float> y = new ArrayList<Float>();
ArrayList<Float> a = new ArrayList<Float>();

ArrayList<Float> px = new ArrayList<Float>();
ArrayList<Float> py = new ArrayList<Float>();
ArrayList<Float> pa = new ArrayList<Float>();

int n = 3, it = 0;
float l = 100, ba = -PI/2, spin = PI/3;
float spn[] = new float[3];

void setup() {
  size(600, 600);
  
  px.add(300);
  py.add(500);
  
  println(px.size());
  for(int i = 0; i < n; i++) {
    spn[i] = map(i,0,n-1,-spin/2,spin/2);
  }
  printArray(spn);
}

void draw() {
  background(0);
  stroke(255);
  for(int i = 0; i < px.size(); i++) {
    for(int j = 0; j < n; j++) {
      line(px.get(0) + l*cos(spn[j]+ba), py.get(0) + l*sin(spn[j]+ba), px.get(0),py.get(0));
    }
  }
  
}

error is here. It says
image

The arraylist expects a float and you are providing an integer, i can run your code by using 300.0 and 500.0 instead of 300 and 500. I guess arraylists do not work like methods and adhere to stricter variable types.

Alternatively you can cast it by using (float)300 and (float)500 or float(300) and float(500).

Alternatively he could just shift to a FloatList container instead, which is both less problematic & more performant: :racing_car:

Or even better, turn all those 6 containers into 1 class. :bulb:

1 Like