BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

How to Convert an array to ArrayList in java

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In the last tutorial, you learned how to convert an ArrayList to Array in Java. In this guide, you will learn how to convert an array to ArrayList.

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 an Array to ArrayList.

Here, we have an array cityNames with four elements. We have converted this array to an ArrayList cityList. After conversion, this arraylist has four elements, we have added two more elements to it using add() method.

In the end of the program, we are printing the elements of the ArrayList, which displays 6 elements, four elements that were added to arraylist from array and 2 new elements that are added using add() method.

import java.util.*;
public class JavaExample {
  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 list after conversion
    cityList.add("Chennai");
    cityList.add("Delhi");

    //print ArrayList elements using advanced for loop
    for (String str: cityList)
    {
      System.out.println(str);
    }
  }
}

Output:

Agra
Mysore
Chandigarh
Bhopal
Chennai
Delhi

Method 2: Conversion using Collections.addAll() method

Collections.addAll() method adds all the array elements to the specified collection. We can call the Collections.addAll method as shown below. It works just like Arrays.asList() method however, it is much faster. Conversion using Collections.addAll() method gives better performance compared to asList() method.

 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 JavaExample {
  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 using addAll()
    Collections.addAll(arraylist, array);

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

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

Output:

Hi
Hello
Howdy
Bye
String1
String2

Method 3: Convert Array to ArrayList manually

This program demonstrates how to convert an Array to ArrayList without using any predefined method such as asList() and addAll().
The logic of this program is pretty simple, we are iterating the array using for loop and adding the element to the ArrayList on every iteration of the loop. This means, in the first iteration, the first element of the array is added to the ArrayList, in the second iteration second element gets added and so on.

To read the whole array, we are using arr.length property. The array.length property returns the number of elements in the array. In the following example, since the array contains four elements, this will return 4. Thus we can say that the for loop runs from i=0 to i<4

In the end, we are displaying ArrayList elements using advanced for loop.

import java.util.*;
public class JavaExample {
  public static void main(String[] args) {

    //ArrayList declaration
    ArrayList<String> arrayList= new ArrayList<String>();

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

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

      //Adding array elements to the ArrayList
      arrayList.add(array[i]);
    }

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

Output:

Text1
Text2
Text3
Text4

Recommended Articles:

  • Array vs ArrayList
  • Make ArrayList read only
  • Sort an ArrayList
❮ Java Collections

Top Related Articles:

  1. ArrayList in Java With Examples
  2. How to empty an ArrayList in Java
  3. Java Scanner class with examples
  4. Difference between length of Array and size of ArrayList in Java
  5. Convert ArrayList to Array in Java

Tags: Collections, Java-Array, Java-ArrayList

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

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 Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap