The Artima Developer Community
Sponsored Link

Java Buzz Forum
Convert arraylist to array in java with example program

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
instanceof java

Posts: 576
Nickname: instanceof
Registered: Jan, 2015

instanceof java is a java related one.
Convert arraylist to array in java with example program Posted: Mar 19, 2017 9:01 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by instanceof java.
Original Post: Convert arraylist to array in java with example program
Feed Title: Instance Of Java
Feed URL: http://feeds.feedburner.com/blogspot/TXghwE
Feed Description: Instance of Java. A place where you can learn java in simple way each and every topic covered with many points and sample programs.
Latest Java Buzz Posts
Latest Java Buzz Posts by instanceof java
Latest Posts From Instance Of Java

Advertisement
  • Inoder to convert arraylist to array normally we will try to iterate arraylist using loop and get each element and put it in an array.
  • But you know we have a predefined method in arraylist which convert list to array of elements in sequence order.
  • toArray() method inside arraylist class.



  1. public <T> T[] toArray(T[] a) {  }

  •  Lets see a java program on how to convert arraylist to array.

 Program #1: Java example program to covert arraylist to array using toArray() method


  1. package arraysinterview;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5. public class ArrayListTOArray {
  6.  
  7.     public static void main(String[] args) {
  8.         List<String> list = new ArrayList<String>();
  9.         
  10.         list.add("array");
  11.         list.add("arraylist");
  12.         list.add("convertion");
  13.         list.add("javaprogram");
  14.         
  15.         String [] str = list.toArray(new String[list.size()]);
  16.         
  17.         for (int i = 0; i < str.length; i++) {
  18.             System.out.println(str[i]);
  19.         }
  20.  
  21.     }
  22.  
  23. }

Output:
  1. array
  2. arraylist
  3. convertion
  4. javaprogram

Program #2: Java example program to covert Integer arraylist to int array using toArray() method

  •  In this case toArray() method gives Integer array so we need to convert again Integer array to int array.



  1. package arraysinterview;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4.  
  5. public class ArrayListTOArray {
  6.  
  7.  public static void main(String[] args) {
  8.         List<Integer> list = new ArrayList<Integer>();
  9.         
  10.         list.add(10);
  11.         list.add(20);
  12.         list.add(30);
  13.         list.add(40);
  14.         list.add(50);
  15.         
  16.         Object[] integers = list.toArray();
  17.         
  18.         int[] intarray = new int[integers.length];
  19.         int i = 0;
  20.         for (Object n : integers) {
  21.             intarray[i++] = (Integer) n;
  22.             System.out.println(i);
  23.         }
  24.  
  25.     }
  26.  
  27. }



Output:
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5

Program #2: Java example program to covert Integer arraylist to int array using toArray() method Using Java 8
  •  

Read: Convert arraylist to array in java with example program

Topic: Format text using printf() method in java Previous Topic   Next Topic Topic: 17% off Ecobee3 Lite Wi-Fi Alexa Compatible Smart Thermostat - Deal Alert

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use