How create a filled ArrayList

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

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

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

2 Likes