1.

How To Convert Arraylist To Array In Java?

Answer»

Within Collection interface there are two versions of toArray() which can be used to convert ARRAYLIST to ARRAY.

  • Object[] toArray()
  • <T&GT; T[] toArray(T array[])

The first version RETURNS an array of Object. 

The second version returns an array containing the elements of the LIST. Normally we go with the second version because it returns the array of the same type as List.

If you have a List called cityList, it can be converted to an array like this -

String cityArray[] = new String[cityList.size()];
cityArray = cityList.toArray(cityArray);

Within Collection interface there are two versions of toArray() which can be used to convert ArrayList to array.

The first version returns an array of Object. 

The second version returns an array containing the elements of the list. Normally we go with the second version because it returns the array of the same type as List.

If you have a List called cityList, it can be converted to an array like this -

String cityArray[] = new String[cityList.size()];
cityArray = cityList.toArray(cityArray);



Discussion

No Comment Found