arrays.aslist string

This tutorial covers difference between Arrays.asList[] and new ArrayList[Arrays.asList] with the help of examples.

Tutorial Contents

  • Understanding Arrays.asList[array]
    • Creates a List Wrapper for Arrays
    • Size of the list is Fixed
    • Modifying one modifies the other
  • Understanding new ArrayList[Arrays.asList[array]]
    • Physically Copies the Elements
    • List can Grow and be Modified Independently
  • Summary

Understanding Arrays.asList[array]

The Arrays provides static utility methods, which are useful to perform various operations on java arrays. We can use Arrays#asList method to create a List instance from an Array.

Before we move ahead, the next snippet shows how to create a list from array using asList method.

String[] stringArray = {"a", "b", "c"}; List stringList = Arrays.asList[stringArray];
Code language: Java [java]

However, the asList method does not convert array, neither it copies elements.

Creates a List Wrapper for Arrays

The method only creates a List wrapper upon the underlying array. Because of which, both the array and the newly created list continue to refer to the exact same elements. That is why, no elements are copied when we use asList method.

Size of the list is Fixed

The arrays have a fixed size, which is specified at the time of creation. Hence, as the newly created list is just a wrapper on that array, the list also follows the fixed size rule.

When we try to add an element into the list, we get UnsupportedOperationException.

List stringList = Arrays.asList[stringArray]; stringList.add["d"];
Code language: Java [java]

Modifying one modifies the other

We can not add more elements to the list, however we can modify existing elements. As both Array and List refer to the same elements, when we modify elements in one of them, it modifies both.

String[] stringArray = {"a", "b", "c"}; List stringList = Arrays.asList[stringArray]; stringArray[1] = "B"; System.out.println[stringList];
Code language: JavaScript [javascript]

As expected the output it prints shows the second element is changed in the list as well.

[a, B, c]

Understanding new ArrayList[Arrays.asList[array]]

We can also convert an array to a list using ArrayList Constructor.

String[] stringArray = {"a", "b", "c"}; List stringList = new ArrayList[Arrays.asList[stringArray]];
Code language: Java [java]

A version constructor of ArrayList can accept a Collection implementation and create new ArrayList containing a copy of the elements from the collection.

Physically Copies the Elements

In the example above, we are passing a list wrapper to the ArrayList constructor. However, the constructor actually copies all the elements and creates a new Arraylist. Unlike the Arrays.asList, using constructor the elements are copied.

List can Grow and be Modified Independently

Once the list is created, we can add more elements into it.

List stringList = new ArrayList[Arrays.asList[stringArray]]; stringList.add["d"];
Code language: Java [java]

Also, we can modify elements in the array, or in the list without changing each other.

In the next example, we are modifying array and list differently.

String[] stringArray = {"a", "b", "c"}; List stringList = new ArrayList[Arrays.asList[stringArray]]; stringArray[1] = "q"; stringList.add[1, "z"];
Code language: Java [java]

If we print both array and list, we get

stringArray -> [a, q, c] stringList -> [a, z, b, c]

Summary

In this short tutorial, we understood the Difference between Arrays.asList[array] and new ArrayList[Arrays.asList[array] methods.

With the help of practical examples, we understood that the list created by the first one is of a fixed size and refer to the same elements as that of array. On the other hand, the list created by the later one, contains copy of all the elements from the array.

For more Java Tutorials please visit: Java Tutorials

Video liên quan

Chủ Đề