What is a quick and easy way to fill a Java array with clones of a single object?
e.g. after:
Rectangle[] rectangles = new Rectangle[N];
fillWithClones(rectangles, new Rectangle(1, 2, 3, 4));
the rectangles
array would contain N distinct Rectangle
instances, initialised with the same coordinates.
I am aware of the flaws of Object.clone()
in Java, but in this case I know that the objects to be copied have non-throwing, public clone()
methods but may or may not have a public copy constructor.
I'm guessing there is a library method somewhere that does this, but I don't think it's in the JDK, Commons-collections or Guava.