4129
Element[] array = {new Element(1), new Element(2), new Element(3)};

How do I convert the above variable of type Element[] into a variable of type ArrayList<Element>?

ArrayList<Element> arrayList = ...;
0

42 Answers 42

1
2
5

Hi you can use this line of code , and it's the simplest way

new ArrayList<>(Arrays.asList(myArray));

or in case you use Java 9 you can also use this method:

List<String> list = List.of("Hello", "Java"); 
List<Integer> list = List.of(1, 2, 3);
Sign up to request clarification or add additional context in comments.

Comments

5

For normal size arrays, above answers hold good. In case you have huge size of array and using java 8, you can do it using stream.

Element[] array = {new Element(1), new Element(2), new Element(3)};
List<Element> list = Arrays.stream(array).collect(Collectors.toList());

Comments

4

Below code seems nice way of doing this.

new ArrayList<T>(Arrays.asList(myArray));

Comments

3

There is one more way that you can use to convert the array into an ArrayList. You can iterate over the array and insert each index into the ArrayList and return it back as in ArrayList.

This is shown below.

public static void main(String[] args) {
        String[] array = {new String("David"), new String("John"), new String("Mike")};

        ArrayList<String> theArrayList = convertToArrayList(array);
    }

    private static ArrayList<String> convertToArrayList(String[] array) {
        ArrayList<String> convertedArray = new ArrayList<String>();

        for (String element : array) {
            convertedArray.add(element);
        }

        return convertedArray;
    }

Comments

3

In java there are mainly 3 methods to convert an array to an arrayList

  1. Using Arrays.asList() method : Pass the required array to this method and get a List object and pass it as a parameter to the constructor of the ArrayList class.

    List<String> list = Arrays.asList(array);                   
    System.out.println(list);
    
  2. Collections.addAll() method - Create a new list before using this method and then add array elements using this method to existing list.

    List<String> list1 = new ArrayList<String>();
    Collections.addAll(list1, array);
    System.out.println(list1);
    
  3. Iteration method - Create a new list. Iterate the array and add each element to the list.

    List<String> list2 = new ArrayList<String>();
    for(String text:array) {
        list2.add(text);
    }
    System.out.println(list2);
    

you can refer this document too

Comments

3

You can use the following 3 ways to create ArrayList from Array.

String[] array = {"a", "b", "c", "d", "e"};

//Method 1
List<String> list = Arrays.asList(array);          

//Method 2
List<String> list1 = new ArrayList<String>();
Collections.addAll(list1, array);

//Method 3
List<String> list2 = new ArrayList<String>();
for(String text:array) {
   list2.add(text);
}

Comments

2

You can create an ArrayList using Cactoos (I'm one of the developers):

List<String> names = new StickyList<>(
  "Scott Fitzgerald", "Fyodor Dostoyevsky"
);

There is no guarantee that the object will actually be of class ArrayList. If you need that guarantee, do this:

ArrayList<String> list = new ArrayList<>(
  new StickyList<>(
    "Scott Fitzgerald", "Fyodor Dostoyevsky"
  )
);

Comments

2

the lambda expression that generates a list of type ArrayList<Element>
(1) without an unchecked cast
(2) without creating a second list (with eg. asList())

ArrayList<Element> list = Stream.of( array ).collect( Collectors.toCollection( ArrayList::new ) );

Comments

1

With Stream (since java 16)

new ArrayList<>(Arrays.stream(array).toList());

Comments

1

I've used the following helper method on occasions when I'm creating a ton of ArrayLists and need terse syntax:

import java.util.ArrayList;
import java.util.Arrays;

class Main {

    @SafeVarargs
    public static <T> ArrayList<T> AL(T ...a) {
        return new ArrayList<T>(Arrays.asList(a));
    }

    public static void main(String[] args) {
        var al = AL(AL(1, 2, 3, 4), AL(AL(5, 6, 7), AL(8, 9)));
        System.out.println(al); // => [[1, 2, 3, 4], [[5, 6, 7], [8, 9]]]
    }
}

Guava uses the same approach so @SafeVarargs appears to be safe here. See also Java SafeVarargs annotation, does a standard or best practice exist?.

Comments

1
Element[] array = {new Element(1), new Element(2), new Element(3)};

List<Element> list = List.of(array);

or

List<Element> list = Arrays.asList(array);

both ways we can convert it to a list.

Comments

0

Use below code

Element[] array = {new Element(1), new Element(2), new Element(3)};
ArrayList<Element> list = (ArrayList) Arrays.asList(array);

Comments

1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.