What is a quick and easy way to populate a Java array with clones of a single object?
eg. after:
Rectangle[] rectangles = new Rectangle[N];
fillWithClones(rectangles, new Rectangle(1, 2, 3, 4));
the array rectangleswill contain N different instances Rectangleinitialized with the same coordinates.
I am aware of the flaws Object.clone()in Java, but in this case I know that the objects to be copied have non-throwing, public methods clone(), but may or may not have an open copy constructor.
I suppose there is some kind of library method that does this, but I don't think it is in the JDK, Commons-collection or Guava.
source
share