Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Java Beginners Arrayexamples Convert ArrayList to Array

Related Tutorials


 
 

Share on Google+Share on Google+

Convert ArrayList to Array

Advertisement
In this section, you will learn about converting ArrayList to Array.

Convert ArrayList to Array

In this section, you will learn about converting ArrayList to Array.

Array is a data structure having fixed size which stores same type of elements in sequential manner. While ArrayList ,extends AbstractList and implements the List interface , supports dynamic array which can grow in size .

Situation comes when you have data in a ArrayList and you want to covert it into array. You can do this using toArray function as shown below :

import java.util.*;

public class ConvertListToArray {
	public static void main(String args[]) {
		List<String> statelist = new ArrayList<String>();
		
		statelist.add("UP");
		statelist.add("Bihar");
		statelist.add("Delhi");
		statelist.add("Kolkata");
		
		String [] states = statelist.toArray(new String[statelist.size()]);
		
		System.out.printf("States Array:\t\t%s", Arrays.toString(states));
	}
}

OUTPUT:

States Array:  [UP, Bihar, Delhi, Kolkata]

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 4, 2013

Related Tutorials

Discuss: Convert ArrayList to Array  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com