+1 vote
1 view
in Java by (830 points)

How do I convert an array to a list in Java?

I used the Arrays.asList() but the behavior (and signature) somehow changed from Java SE 1.4.2 (docs now in archive) to 8 and most snippets I found on the web use the 1.4.2 behaviour.

For example:

int[] spam = new int[] { 1, 2, 3 };
Arrays.asList(spam)
  • on 1.4.2 returns a list containing the elements 1, 2, 3
  • on 1.5.0+ returns a list containing the array spam

In many cases it should be easy to detect, but sometimes it can slip unnoticed:

Assert.assertTrue(Arrays.asList(spam).indexOf(4) == -1);

2 Answers

0 votes
by (13.2k points)
edited by

List cannot operate on primitive data type, int is a primitive data type. Therefore, as in your example List<int> is not possible.

As an alternative you can use List<Integer> . Integer is a class data type which includes int primitive data type in it.To convert your array to a list use Arrays.asList() method.

So, your code should be

Integer[] spam = new Integer[] { 1, 2, 3 };

List<Integer> list = Arrays.asList(spam);

0 votes
by (46.1k points)

Here are the methods to convert Array to List in Java:

  1. Brute Force or Naive Method: In this process, an empty List is designed and all elements present of the Array are attached to it one by one.

Algorithm:

  • Get the Array to be transformed.

  • Build an empty List

  • Iterate over the items in the Array.

  • For all items, attach it to the List

  • Pass the formed List

Syntax:

import java.util.*; 

import java.util.stream.*; 

  

class GFG { 

  

    public static <T> List<T> convertArrayToList(T array[]) 

    {

   List<T> list = new ArrayList<>(); 

  

        // Iterate over the array 

        for (T t : array) { 

            // Add all elements into the list 

            list.add(t); 

        } 

          return list; 

    } 

  

    public static void main(String args[]) 

    { 

        // Create an Array 

        String array[] = { "Fire", "onFire", 

                                    "A Song" }; 

  

        // Print the Array 

        System.out.println("Array: " 

                       + Arrays.toString(array)); 

  

        // convert the Array to List 

        List<String> 

            list = convertArrayToList(array); 

  

        // Print the List 

        System.out.println("List: " + list); 

    } 

Output:

Array: [Fire, onFire, A Song]

List: [Fire, onFire, A Song]

2. Using Arrays.asList() method: In this method, the Array is transferred as the    parameter into the List constructor with the help of Arrays.asList() program.

Algorithm:

  • Get the Array to be transformed.

  • Design the List by giving the Array as a parameter in the constructor of the List with the help of Arrays.asList() method

  • Pass the formed List

Syntax:

  import java.util.*; 

import java.util.stream.*; 

  

class GFG { 

  

    public static <T> List<T> convertArrayToList(T array[]) 

    { 

  

       

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

  

        // Return the converted List 

        return list; 

    } 

  

    public static void main(String args[]) 

    { 

        // Create an Array 

        String array[] = { "Fire", "onFire", 

                                    "A Song" }; 

  

        // Print the Array 

        System.out.println("Array: " 

                           + Arrays.toString(array)); 

  

        // convert the Array to List 

        List<String> 

            list = convertArrayToList(array); 

  

        // Print the List 

        System.out.println("List: " + list); 

    } 

Output:

Array: [Fire, onFire, A Song]

List: [Fire, onFire, A Song]

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 27 in Java by Ritik (3.5k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...