Related Examples
 

Java LinkedList toArray Example

LinkedList class toArray method example. This example shows you how to use toArray method.


LinkedList class toArray method example. public T[] toArray(T[] a) Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

Here is the code

/**
 * @ # toArray1.java
 * A class repersenting use to toArray method of LinkedList class in java.util package
 * version 14 May 2008
 * author Rose India 
 */
import java.util.*;

public class ToArray1 {

    public static void main(String args[]) {
        List list = new LinkedList();
        int i = 0;
        String str = "Rose";

        //Add the specified element to the end of this list.
        list.add(i);
        list.add(str);
        list.add("India");
        System.out.println("list = " + list);

        Object arr[] {"Komal""Choudhary"};
        System.out.println("Elements of arr ");
        for (int a = 0; a < arr.length; a++) {
            System.out.println(arr[a].toString());
        }

        // The existing elements in arr are replaced with the
        // contents of the LinkedList.
        arr = list.toArray(arr);
        System.out.println("\nElements of arr ....... ");
        for (int a = 0; a < arr.length; a++) {
            System.out.println(arr[a].toString());
        }
    }
}

Output
list = [0, Rose, India]
Elements of arr
Komal
Choudhary

Elements of arr .......
0
Rose
India

Post Comment
Name:
E-mail:
Contact no :
Comments:
  Refresh Image
Verify Image:
 
 
Your Comment's
 
 
 

HOME | COPYRIGHT | CONTACT US