How to Convert Array List to Array in Java

Published on Oct 30,2019 1.6K Views

How to Convert Array List to Array in Java

edureka.co

The Array List is a subset of the collection of frameworks, which is present in “java.util” package. It illustrates a dynamic array in Java. Although, it can be slower than standard arrays but it sure seems to be helpful in programs where numerous manipulation in the array is needed

 

Features of Array Lists

The Java Array Lists constitute of Constructors and Methods. The below details mentioned is a list of few constructors and methods along with their use and functions.

Let us look at a simple code to create an Array List.

Example:

import java.io.*;
import java.util.*;

class arrayli {
      public static void main(String[] args) throws IOException {
           int n = 5;
           ArrayList<Integer> arrli = new ArrayList<Integer>(n);
           for (int i = 1; i <= n; i++)
                 arrli.add(i);
           System.out.println(arrli);
           arrli.remove(3);
           System.out.println(arrli);
           for (int i = 0; i < arrli.size(); i++)
                 System.out.print(arrli.get(i) + " ");
      }
}

//Output:

[1, 2, 3, 4, 5]
[1, 2, 3, 5]
1 2 3 5

Some common methods in Java 

 

Converting Array List to Array () syntax.

There are two methods:

After we fill all array elements, it has more space left in the array. Then ‘null’ is populated in all those extra positions.

 

The code of the corresponding output is placed below this output. 

Example:

import java.util.ArrayList;
import java.util.Arrays;
 
public class ArrayListExample {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>(2);
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        Object[] array = list.toArray();
        System.out.println( Arrays.toString(array) );
        for(Object o : array) {
            String s = (String) o;
            System.out.println(s);
        }
    }
}

//Output:

[A, B, C, D]

A
B
C
D

 

Example:

import java.util.ArrayList;
import java.util.Arrays;
 
public class ArrayListExample{
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>(2);
        list.add("A");
        list.add("B");
        list.add("C");
        list.add("D");
        String[] array = list.toArray(new String[list.size()]);
        System.out.println(Arrays.toString(array));
    }
}

//Output:

[A, B, C, D]

 

With this, we come to an end of this article. I hope you have understood the Array List to Array in Java, their types, importance and their implementation through some real-time examples.

Now that you have understood the basics of Array List to Array in Java, check out the Java training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. Edureka’s Java J2EE and SOA training and certification course is designed for students and professionals who want to be a Java Developer. The course is designed to give you a head start into Java programming and train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Mention it in the comments section of this “Array List to Array in Java” blog and we will get back to you as soon as possible.

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 1st March,2025

1st March

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Building Robust Applications with Spring Framework