How To Convert Array To Linkedlist Flashcards

1
Q

How do you convert an array to a linked lists

A

import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedList;

/**
* Java 1.4+ Compatible
* The following example code demonstrates converting an Array of Strings to a LinkedList of Strings
*/
public class Array2LinkedList {
public static void main(String[] args) {
        // initialize array with some data
        String[] sa = new String[] { "A", "B", "C" };
        // convert array to LinkedList
        LinkedList ll = new LinkedList(Arrays.asList(sa));
        // iterate over each element in LinkedList and show what is in the list.
        Iterator iterator = ll.iterator();
        while (iterator.hasNext()) {
            // Print element to console
            System.out.println((String) iterator.next());
        }
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly