Tuesday, June 17, 2008

Conversion between Array and List types

In some scenario, we may need to convert an array to a list or vice versa. We can use asList() method available in the Arrays class, and the toArray() method in List and set classes serve this purpose.

The Arrays.asList() method converts an array into a list and the size of the list is fixed.

The toArray() method converts a set or list to an array. This method has two forms of invocation. One form of the toArray() method returns a Object array, and another form returns the array of the type that is passed as the argument.

0 Java Junction comments: