Difference between Collection.toArray() and Collection.toArray(Object obj[])
arrays, java
Solution
One is generic, the other isn't. `toArray()` will return `Object[]` while `toArray(T[])` will return an array of type `T[]`.
Sample:
public static void main(String[] args) {
Object[] baseArray = new ArrayList<String>().toArray();
System.out.println(baseArray.getClass().getCanonicalName());
String[] improvArray = new ArrayList<String>().toArray(new String[5]);
System.out.println(improvArray.getClass().getCanonicalName());
}
Output:
java.lang.Object[]
java.lang.String[]
Problem
According to java doc for toArray() Returns an array containing all of the elements in this collection. and toArray(Object obj[]). Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array. first toArray() i understand but second toArray(Object obj[]) i can't understand.Please explain with example.