initialize an array object in java code example
Example: java initialize object array
import java.util.stream.Stream;
class Example {
public static void main(String[] args) {
int len = 5; // For example.
// Use Stream to initialize array.
Foo[] arr = Stream.generate(() -> new Foo(1)) // Lambda can be anything that returns Foo.
.limit(len)
.toArray(Foo[]::new);
}
// For example.
class Foo {
public int bar;
public Foo(int bar) {
this.bar = bar;
}
}
}