About 38,100 results
Open links in new tab
  1. Convert list to array in Java - Stack Overflow

    There are two styles to convert a collection to an array: either using a pre-sized array (like c.toArray(new String[c.size()])) or using an empty array (like c.toArray(new String[0]). In older …

  2. Java: how to implement `toArray` for `Collection` - Stack Overflow

    Oct 25, 2010 · All you can do is cast to (T), the cast() method only changes the type of the reference, it won't change the type of the object referenced. – Peter Lawrey Commented Oct …

  3. Java List <T> T[] toArray(T[] a) implementation - Stack Overflow

    Mar 15, 2013 · The reason why the method has this signature is because the toArray API predates generics: the method. public Object[] toArray(Object[] a) has been introduced as …

  4. arrays - ArrayList.toArray () method in Java - Stack Overflow

    Mar 27, 2012 · The way the method is defined means that you can pass in a reference to an existing array, and have this populated via the method. In some cases this is likely very …

  5. java - why does List<String>.toArray() return Object[] and not …

    Because arrays have been in Java since the beginning, while generics were only introduced in Java 5. And the List.toArray() method was introduced in Java 1.2, before generics existed, and …

  6. java - .toArray(new MyClass[0]) or .toArray(new …

    Oct 6, 2008 · In either case, calling toArray(new MyClass[0]) on the collection directly is likely to be faster. (And to consider APIs introduced after your question, i.e. JDK 11+, calling …

  7. java - From Arraylist to Array - Stack Overflow

    Nov 1, 2011 · The Collection interface includes the toArray() method to convert a new collection into an array. There are two forms of this method. The no argument version will return the …

  8. arrays - Java generics in ArrayList.toArray () - Stack Overflow

    Apr 13, 2016 · The toArray(T[] a) method either fills the given array (if the size matches) or creates a new new array using the type of the parameter, which is the array-type of the …

  9. java - The easiest way to transform collection to array ... - Stack ...

    @Carlos actually, it uses better memory than (new Foo[0]).As per the Collection.toArray documentation, ` @param a the array into which the elements of this collection are to be …

  10. java - make arrayList.toArray () return more specific types - Stack ...

    Feb 21, 2011 · String[] a = list.toArray(new String[list.size()]); because the internal implementation would realloc a properly sized array anyway so you were better doing it upfront. Since Java 6 …

Refresh