Copy LinkedList to an Array object Example

Copy LinkedList to an Array object Example


Posted in : Java Posted on : November 30, 2010 at 5:39 PM Comments : [ 0 ]

This Java Example shows how to copy all elements of LinkedList object to an Array object in java.

Copy LinkedList to an Array object Example

This Java Example shows how to copy all elements of LinkedList object to an Array object  in java. To copy the LinkedList elements to Array object we use Object[] toArray( ) method. This method returns an array containing all of the elements in this list in proper sequence.

 Example: LinkedListcopytoArray.java

package devmanuals.com;

import java.util.*;

public class LinkedListcopytoArray {
	public static void main(String[] args) {
		LinkedList LKLST = new LinkedList();
		LKLST.add("Gyan");
		LKLST.add("Singh");
		LKLST.add("Arunesh");
		LKLST.add("Kumar");
		Object[] obj = LKLST.toArray();
		System.out.println("The Array Elements are : ");
		for (int i = 0; i < obj.length; i++) {
			System.out.println(obj[i]);
		}
	}
}

Output:

The Array Elements are : 

Gyan

Singh 

Arunesh 

Kumar

Download The Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics