Java/J2EE and SOA (325 Blogs) Become a Certified Professional
AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (41 Blogs)
  • Comprehensive Java Course (2 Blogs)
  • Java/J2EE and SOA (324 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

How to Convert Array List to Array in Java

Published on Oct 30,2019 144 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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 Array List inherits Abstract List class and implements the List interface.
  • The Array List is initialized by size; however, its size can increase if collection grows or shrinks if objects are extracted from the collection.
  • The Java Array List provides us with random access to the lists.
  • Array Lists cannot be utilized for older types, like int, char, etc. A wrapper class is used for such cases. 
  • The Array List in Java can be seen as similar to vectors in C++.

Array list to array in java picture 1

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.

  • ArrayList(): This constructor is utilized to create an empty array list
  • ArrayList(Collection c): This constructor is utilized to create an array list initialized with elements from collection c.
  • ArrayList(int capacity): This constructor is utilized to create an array list with an initial capacity being specified.

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 

  • forEach​(Consumer action): This performs a specific action for each element of the repetitive factor until all elements have been processed or an action throws an exception.
  • retainAll​(Collection c): This retains only the elements in this list which are contained in a specific collection.
  • removeIf​(Predicate filter): This extracts all of the elements of the collection that satisfy the given predicate.
  • contains​(Object o): This returns true if a list has the specified element.
  • remove​(int index): This removes the element at a given specific position in this list.
  • remove​(Object o): This removes the initial occurrence of a specified element from this list if it is present.
  • get​(int index): It returns the element at a specific position in this list.
  • subList​(int fromIndex, int toIndex): It returns a portion of this list between the specified from Index, inclusive, and to Index, exclusive etc.
  • spliterator​(): It creates a late-binding and fail-fast Split iterator over the elements in this list.

 

Converting Array List to Array () syntax.

There are two methods:

  • The first method will not accept any argument and returns an array of the object type. It is our responsibility to iterate the objects array, find the desired element and typecast to the class type we want.
  • In the second method, the runtime type of a returned array is of a specified array. If a list fits in a specified array, it will be returned in it. Else, a new array is immediately allocated with a runtime type of a specified array and the size of this list.

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

 

  • Array List to Array() – Convert to Object Array

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

 

  • Array List to Array (T[] a)  – Convert to the string array

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.

Comments
0 Comments

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.