"Mike Schilling" <mscottschill...@hotmail.com> wrote in message
news:Z7E5i.6841$4Y.6769@newssvr19.news.prodigy.net...
<blm...@myrealbox.com> wrote in message
news:5bog4pF2u1ladU2@mid.individual.net...
In article <1180107837.234928@news1nwk>,
Eric Sosman  <Eric.Sos...@Sun.COM> wrote:
blm...@myrealbox.com wrote On 05/25/07 07:19,:
is
there some neat trick I don't know for creating an array of
objects without an explicit loop?
Thing[] thing = new Thing[] {
    new Thing(42),
    new Thing("abc"),
    new SubclassOfThing(new Date()),
};
Well, you got me on that one!  But I can't think how to state the
question I actually had in mind more precisely, so -- oh well.
I think you're saying that there's no way to create a array of objects
whose size is determined at run-time without a loop.
public class Test4 {
  public static Object[]
makeArrayOfObjectsWhoseSizeIsDeterminedAtRuntimeWithoutALoop(int size) {
    if (size < 0) {
      throw new IllegalArgumentException();
    }
    if (size == 0) {
      return new Object[0];
    }
    Object[] returnValue = new Object[size];
    returnValue[0] = new Object();
    System.arraycopy(makeArrayOfObjectsWhoseSizeIsDeterminedAtRuntimeWithoutALoop(size
 - 1), 0, returnValue, 1, size - 1);
    return returnValue;
  }
}
    Or, since array indexes must be java integer, and there's a finite
number of them:
public class Test4 {
  public static Object[]
makeArrayOfObjectsWhoseSizeIsDeterminedAtRuntimeWithoutALoop(
      int size) {
    switch (size) {
    case 0:
      return new Object[] {};
    case 1:
      return new Object[] { new Object() };
    case 2:
      return new Object[] { new Object(), new Object() };
    /*etc.*/
    default:
      throw new IllegalArgumentException();
    }
  }
}
    - Oliver