Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

5
  • Thanks, this is very close to what i am looking for. I gave my example with Object but what if i want to use another type like Integer or Date ? Commented Aug 1, 2014 at 10:12
  • 5
    @gontard: For generating an array of MyType, use toArray(MyType[]::new) Commented Aug 1, 2014 at 10:15
  • Ok. Could you add this alternative to your answer ? A last question: is it possible to do the same thing with a generic type ? I know it is not possible to create an array of generics but may be... Commented Aug 1, 2014 at 10:18
  • 3
    As a remark, you can fill an already existing array in the following “creative” way: Arrays.asList(array).replaceAll(x->supplier.get());. You can even overwrite a range using subList. Commented Aug 1, 2014 at 11:32
  • Using the creative way one should be careful with primitive types. First example is ok: System.out.println(Arrays.asList( new Integer[]{1,2,3,4,5})); [1, 2, 3, 4, 5] Second example is a list containing one single element which is the whole array: System.out.println(Arrays.asList( new int[]{1,2,3,4,5})); [[I@53b32d7] Commented Oct 22, 2019 at 5:35