LinkedList Object[] toArray() method Example Program


Returns an array containing all of the elements in this list in proper sequence (from first to last element).
The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.This method acts as bridge between array-based and collection-based APIs.

Program

package com.candidjava;

import java.util.LinkedList;

/**
 * 
 * @author : mohan
 * @description :The java.util.LinkedList.toArray() method returns an array
 *              containing all of the elements in this list in proper sequence
 *              (from first to last element).This method acts as bridge between
 *              array-based and collection-based APIs.
 */
public class LinkedListToArrays {

	public static void main(String[] args) {

		LinkedList list = new LinkedList();

		list.add("anandh");
		list.add("mohan");
		list.add("arun");
		list.add("vinoth");

		System.out.println("LinkedList:" + list);
		Object[] array = list.toArray();
		for (int i = 0; i < list.size(); i++) {
			System.out.println("Array:" + array[i]);
		}
	}
}

Output

LinkedList:[anandh, mohan, arun, vinoth]
Array:anandh
Array:mohan
Array:arun
Array:vinoth

Explanation

public Object[] toArray()
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.This method acts as bridge between array-based and collection-based APIs.

Specified by:
toArray in interface Collection<E>

Specified by:
toArray in interface List<E>

Overrides:
toArray in class AbstractCollection<E>

Returns:
an array containing all of the elements in this list in proper sequence

See Also:
Arrays.asList(Object[])


Related Post

Comments


©candidjava.com