# How create a filled ArrayList

**URL:** <https://discourse.processing.org/t/how-create-a-filled-arraylist/32721>\
**Category:** Beginners\
**Created:** [October 10, 2021, 8:22am UTC](https://discourse.processing.org/t/how-create-a-filled-arraylist/32721 "2021-10-10T08:22:36Z")\
**Posts on this page:** 3\
**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:** [October 10, 2021, 8:22am UTC](https://discourse.processing.org/t/how-create-a-filled-arraylist/32721/1 "2021-10-10T08:22:36Z")

</div>

Hi!  
I am trying to make a game for fun. It is about exploring rooms and fighting enemies (similar to ‘enter the gungeon’, but completely 2D, rooms require teleportation to switch and way less mechanics)

What I am trying to do is:  
While creating a room (object) I pass into it the arguments I want. One of those is an ArrayList which is filled with objects. Would it be possible to fill the arraylist while it is being created?

How I imagine it would look like:

```auto
room r = new room(0, 600, 600, new ArrayList<obstacle>()[]{new obstacle(1,2,3,4)});
//taking inspiration from:
println(new obstacle[]{new obstacle(1,2,3,4)});

```

Is it even possible to do it?

Now that I think of it, passing an ArrayList wouldn’t be absolutely needed, since I can just pass a normal 1D array[] and add all of the items to an empty ArrayList but still.

Thank you!

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [October 10, 2021, 10:01am UTC](https://discourse.processing.org/t/how-create-a-filled-arraylist/32721/2 "2021-10-10T10:01:38Z")

</div>

i don’t know if i follow but you mean something like

```auto
import java.util.Arrays;

class Obstacle
{
  int foo;
  Obstacle(int foo) {
    this.foo = foo;
  }
}

Obstacle[] obstaclesArray = new Obstacle[]{new Obstacle((int)random(100)), new Obstacle((int)random(100))};

ArrayList<Obstacle> obstaclesArrayList = new ArrayList<Obstacle>(Arrays.asList(obstaclesArray));

for(Obstacle ob : obstaclesArrayList) {
  println(ob.foo);
}

```

or sticking closer to your example

```auto
import java.util.Arrays;

class Obstacle {
  int foo;
  Obstacle(int foo) {
    this.foo = foo;
  }
}

class Room {
  int foo1;
  float foo2;
  ArrayList<Obstacle> obstacles;
  
  Room(int foo1, float foo2, ArrayList<Obstacle> obstacles) {
    this.foo1 = foo1;
    this.foo2 = foo2;
    this.obstacles = obstacles;
  }
}

Room room1 = new Room((int)random(100), random(100), new ArrayList<>(Arrays.asList(new Obstacle[]{new Obstacle((int)random(100)), new Obstacle((int)random(100))})));

println(room1.foo1);
println(room1.foo2);
println(room1.obstacles.get(0).foo);
println(room1.obstacles.get(1).foo);

```

but your last idea is probably the best approach

---

<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:** [October 10, 2021, 10:21am UTC](https://discourse.processing.org/t/how-create-a-filled-arraylist/32721/3 "2021-10-10T10:21:12Z")

</div>

> **[ArrayList in Java - GeeksforGeeks](https://www.geeksforgeeks.org/arraylist-in-java/)**
>
> A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

I do like the flexibility of arralists sometimes you just don’t know the number of items you will have and whilst you could get around this buy initialising a larger array than necessary arraylists require no such additional code.

Do bear in mind that ints have to be initialised as Integer same with floats just something you have to be mindful of.

Essentially though an arraylist is either created empty or null, if its empty then you have to then fill it using add(). If it’s null you first have to initialise it ie make it empty. Of course you can create a null arraylist and set as another live array, although from recollection I may be wrong, doing so has the same limitation as setting an array to another array meaning that the arraylist will simply be a symbolic link to the original and will not in fact be a copy.

Again something to keep in mind

You can also have arraylists of arraylists arraylists of hashmaps or any other type of object that is normally handled in java.
