Advertisement
makispaiktis

I3. Convert: Array <---> LinkedList

Jun 6th, 2022 (edited)
1,179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.67 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class myClass {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.  
  8.     String[] stuff = {"babies", "watermelon", "melons", "fudge"};
  9.     // Convert a string array into a linked list with this constructor
  10.         // Reason: I CAN USE LIST'S METHODS INSTEAD OF ARRAY'S
  11.     LinkedList<String> thelist = new LinkedList<String>(Arrays.asList(stuff));
  12.     System.out.println(thelist);
  13.     thelist.add("pumpkin");
  14.     thelist.addFirst("first");
  15.     System.out.println(thelist);
  16.     // Convert back to an array
  17.     stuff = thelist.toArray(new String[thelist.size()]);
  18.     for(String x : stuff){
  19.         System.out.printf("%s ", x);
  20.     }
  21.  
  22.     }
  23.  
  24. }
  25.  
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement