Create a List of a specific type from an array of the same specific type

This example shows how to create a List which holds objects of a specific type from an array which holds elements of the same type. Instead of converting to a List you could as easily convert to any type of list, like ArrayList, as long as it implements the List interface. Or more generally, we could say that we convert T[] to List, where the letter T represents Type.

In the example below we create an array with four elements which are instances of class Person. Then we call the method Arrays.asList and pass in the array as parameter. The result is assigned to a variable which is of type List. Then we print out the results from the List. Further more we override the toString method in the Person class to be able to get a formatted output.

The output from the code is:

Search for more Java info on this site here:
Custom Search

Please type any questions here.