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

Convert ArrayList to Array in Java

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

In this tutorial, you will learn how to convert ArrayList to Array in Java. We will see two ways to do the conversion: In the first program, we will do the conversion without using any method. In the second program, we will use toArray() method of ArrayList class to do the conversion.

Example 1: ArrayList to Array conversion without using toArray() method

Steps followed in this program are:
1. Create an array with the same size as the arraylist. The arraylist size can be found using size() method of ArrayList class. The statement String arr[] = new String[arrList.size()]; creates an array arr of string type with the same size as of arrList.
2. Run a for loop from 0 till the arraylist size and at every iteration get arraylist element using get() method and assign it to the array arr.
3. Once all elements are added to the array, print array elements using enhanced for loop.

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

    //ArrayList declaration and initialization
    ArrayList<String> arrList= new ArrayList<>();
    arrList.add("String1");
    arrList.add("String2");
    arrList.add("String3");
    arrList.add("String4");

    //ArrayList to Array Conversion
    String arr[] = new String[arrList.size()];
    for(int j =0;j<arrList.size();j++){
      arr[j] = arrList.get(j);
    }

    System.out.println("Original ArrayList: "+ arrList);
    System.out.println("Array Elements:");
    //Displaying Array elements
    for(String k: arr)
    {
      System.out.println(k);
    }
  }
}

Output:
Java ArrayList to Array Conversion

Example 2: ArrayList to Array conversion using toArray() method

In this example, we are using a predefined method toArray() of ArrayList class. This method converts an ArrayList and returns an equivalent array. This is simple compared to the example 1, as here we do not have to write any logic and simply call the method toArray() to get an equivalent array.

Here we have an arraylist names, this list contains names. We are converting this arraylist to an array using toArray().

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

    //An ArrayList of strings
    ArrayList<String> names= new ArrayList<>();
    names.add("Ankur");
    names.add("Ajeet");
    names.add("Harsh");
    names.add("John");

    // ArrayList to Array Conversion using toArray()
    String arr[]=names.toArray(new String[names.size()]);

    //Print array elements
    for(String str: arr)
    {
      System.out.println(str);
    }
  }
}

Output:

Ankur
Ajeet
Harsh
John

Example 3: ArrayList of Integer type to an int array

In this example, we have an array of integer type. This array contains integers as elements. We are converting this arraylist to an int array using the toArray() method.

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

    //An ArrayList of integers
    ArrayList<Integer> numbers= new ArrayList<>();
    numbers.add(11);
    numbers.add(22);
    numbers.add(33);
    numbers.add(44);
    numbers.add(55);

    // ArrayList to Array Conversion using toArray()
    Object arr[]=numbers.toArray();

    //Print array elements
    for(Object num : arr)
    {
      System.out.println(num);
    }
  }
}

Output:

11
22
33
44
55
❮ Java ArrayListJava Collections ❯

Top Related Articles:

  1. How to loop HashMap in java
  2. Add Multiple Items to an ArrayList in Java
  3. Convert a Set of String to a comma separated String in Java
  4. How to Compile and Run your First Java Program
  5. Java Scanner class with examples

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

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