# Problem with ArrayList\<Float\>

**URL:** <https://discourse.processing.org/t/problem-with-arraylist-float/26215>\
**Category:** Beginners\
**Created:** [December 13, 2020, 9:57pm UTC](https://discourse.processing.org/t/problem-with-arraylist-float/26215 "2020-12-13T21:57:44Z")\
**Posts on this page:** 4\
**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 13, 2020, 9:57pm UTC](https://discourse.processing.org/t/problem-with-arraylist-float/26215/1 "2020-12-13T21:57:44Z")

</div>

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.

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

```

---

<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 13, 2020, 9:58pm UTC](https://discourse.processing.org/t/problem-with-arraylist-float/26215/2 "2020-12-13T21:58:39Z")

</div>

> [@CodeMasterX](#):
>
> ```auto
> px.add(300);
> py.add(500);
> 
> ```

error is here. It says  
 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/3/3506ba3a316a4491a239d091c121934e21fb6f1b.png)

---

<div class="post-metadata">

**Author:** ![paulgoux](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@paulgoux](https://discourse.processing.org/u/paulgoux)\
**Post date:** [December 13, 2020, 10:03pm UTC](https://discourse.processing.org/t/problem-with-arraylist-float/26215/3 "2020-12-13T22:03:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [December 13, 2020, 10:08pm UTC](https://discourse.processing.org/t/problem-with-arraylist-float/26215/4 "2020-12-13T22:08:19Z")

</div>

> [@paulgoux](#):
>
> Alternatively you can cast it…

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

> **[FloatList / Reference](https://processing.org/reference/FloatList.html)**
>
> Helper class for a list of float values. Lists are designed to have some features of ArrayList, but to maintain the simplicity and efficiency of working with arrays. Functions such as

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