how to initiate an array of objects java code example
Example 1: java initialize object array
import java.util.stream.Stream;
class Example {
public static void main(String[] args) {
int len = 5;
Foo[] arr = Stream.generate(() -> new Foo(1))
.limit(len)
.toArray(Foo[]::new);
}
class Foo {
public int bar;
public Foo(int bar) {
this.bar = bar;
}
}
}
Example 2: how to make array of objects in java and use it
class enemies {
int marks;
}
enemies[] enemiesArray = new enemies[7];
enemiesArray[5] = new enemies(95);