beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

How to convert an array to ArrayList in java

By Chaitanya Singh | Filed Under: Java Collections

In the last tutorial we have shared two methods of converting an ArrayList to Array with example. Here we are sharing three different ways to convert an Array to ArrayList. Basically we are converting an String Array to ArrayList of String type.

String array[] to ArrayList<String>

Method 1: Conversion using Arrays.asList()

Syntax:

ArrayList<T> arraylist= new ArrayList<T>(Arrays.asList(arrayname));

Example:

In this example we are using Arrays.asList method to convert the Array to ArrayList.

import java.util.*;

public class ArrayToArrayList {
     public static void main(String[] args) {

	  /* Array Declaration and initialization*/
	  String citynames[]={"Agra", "Mysore", "Chandigarh", "Bhopal"};

	  /*Array to ArrayList conversion*/
	  ArrayList<String> citylist= new ArrayList<String>(Arrays.asList(citynames));

	  /*Adding new elements to the converted List*/
	  citylist.add("New City2");
	  citylist.add("New City3");

	  /*Final ArrayList content display using for*/
	  for (String str: citylist)
	  {
		System.out.println(str);
       	  }
      }
}

Output:

Agra
Mysore
Chandigarh
Bhopal
New City2
New City3

Method 2: Collections.addAll method

Collections.addAll method all the array elements to the specified collection. This is how Collections.addAll method is being called. It does the same as Arrays.asList method however it is much faster than it so performance wise this is a best way to get the array converted to ArrayList.

String array[]={new Item(1), new Item(2), new Item(3), new Item(4)};
ArrayList<T> arraylist = new ArrayList<T>();
Collections.addAll(arraylist, array);

OR

Collections.addAll(arraylist, new Item(1), new Item(2), new Item(3), new Item(4));

Example

import java.util.*;

public class Example2 {
	public static void main(String[] args) {

	    /* Array Declaration and initialization*/
	    String array[]={"Hi", "Hello", "Howdy", "Bye"};

	    /*ArrayList declaration*/
	    ArrayList<String> arraylist= new ArrayList<String>();

	    /*Conversion*/
	    Collections.addAll(arraylist, array);

	    /*Adding new elements to the converted List*/
	    arraylist.add("String1");
	    arraylist.add("String2");

	    /*Display array list*/
	    for (String str: arraylist)
	    {
	 	System.out.println(str);
	    }
	}
}

Output

Hi
Hello
Howdy
Bye
String1
String2

Method 3: Manual way of doing things

We can also add all the array’s element to the array list manually. Below example shows the logic of manual conversion.

package beginnersbook.com;

import java.util.*;

public class Details {
	public static void main(String[] args) {

	    /*ArrayList declaration*/
	    ArrayList<String> arraylist= new ArrayList<String>();

	    /*Initialized Array*/
	    String array[] = {"Text1","Text2","Text3","Text4"};   

	    /*array.length returns the current number of 
	     * elements present in array*/
	    for(int i =0;i<array.length;i++)
            {

	         /* We are adding each array's element to the ArrayList*/
		 arraylist.add(array[i]);
	    }

	    /*ArrayList content*/
	    for(String str: arraylist)
	    {
	         System.out.println(str);
	    }
      }
}

Output:

Text1
Text2
Text3
Text4

Enjoyed this post? Try these related posts

  1. Java – Add elements at beginning and end of LinkedList example
  2. How to convert ArrayList to string array in java
  3. Java ArrayList addAll(Collection c) Method example
  4. Comparator Interface in Java
  5. Deque Interface in Java Collections
  6. Vector Iterator example in Java

Comments

  1. Saheb Bhattu says

    July 1, 2015 at 6:10 PM

    In Method 2 ,
    can’t we use this method to convert array to arraylist :
    arraylist.addAll(array);

    as we have used it earlier for list :
    arraylist.addAll(list);

    Reply
  2. Shivam says

    July 31, 2015 at 7:37 PM

    Hello,
    The method to convert an array to ArrayList using Arrays.asList() works well for any String array, but not with Integer type arrays.

    Please help.
    Thanks

    Reply
    • Kishan says

      March 24, 2016 at 12:37 PM

      If you are using the array as “int[] obj=new int[x];” then it will not work because the array list can be created for the Integer class not the int class so it will return a type mismatch error. so take the array as Integer[] obj=new Integer[x];

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

  • Java Tutorial
  • OOPs Concepts

Java Collections

  • ArrayList
  • LinkedList
  • ArrayList vs LinkedList
  • Vector
  • ArrayList vs Vector
  • HashMap
  • TreeMap
  • LinkedHashMap
  • HashSet
  • TreeSet
  • LinkedHashSet
  • Hashtable
  • HashMap vs Hashtable
  • Queue
  • PriorityQueue
  • Deque & ArrayDeque
  • Iterator
  • ListIterator
  • Comparable Interface
  • Comparator Interface
  • Java Collections Interview Q

MORE ...

  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap