Array and List in Java

In Java, Array and List have a lot in common. Now I am going to introduce how to convert them to each other.


How to convert List to Array?

1
2
List<T> list = new ArrayList<T>();
T [] array = list.toArray(new T[list.size()]);

How to convert Array to List?

There are two ways to convert Array to List.

  • Flexible Length Way (Recommended)
    1
    List list = new ArrayList(Arrays.asList(array));

The above is recommended because it returns an ArrayList which has flexible length (you can add extra elements).

  • Easy but Inflexible Way
    1
    List list = Arrays.asList(array);

The End