Advertisement
makispaiktis

I2. LinkedList Methods - addAll, previous, next, subList, clear

Jun 6th, 2022 (edited)
1,302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.39 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class myClass {
  4.  
  5.     public static void main(String[] args){
  6.  
  7.         String[] things = {"apples", "noobs", "pwnge", "bacon", "goATS"};
  8.         List<String> list1 = new LinkedList<String>();
  9.         for(String x : things){
  10.             list1.add(x);
  11.         }
  12.         System.out.println(list1);
  13.         String[] things2 = {"sausage", "bacon", "goats", "hattypotter"};
  14.         List<String> list2 = new LinkedList<String>();
  15.         for(String y : things2){
  16.             list2.add(y);
  17.         }
  18.         System.out.println(list2);
  19.  
  20.         // BUILT-IN METHODS
  21.         // addAll, previous, next, subList, clear
  22.         list1.addAll(list2);
  23.         list2 = null;           // Free space
  24.         printMe(list1);
  25.         removeStuff(list1, 2, 5);
  26.         printMe(list1);
  27.         reverseMe(list1);
  28.  
  29.  
  30.  
  31.     }   // END OF MAIN
  32.  
  33.     private static void reverseMe(List<String> list1) {
  34.         ListIterator<String> it = list1.listIterator(list1.size());
  35.         while(it.hasPrevious()){
  36.             System.out.printf("%s ", it.previous());
  37.         }
  38.     }
  39.  
  40.     private static void removeStuff(List<String> list1, int from, int to) {
  41.         list1.subList(from, to).clear();
  42.     }
  43.  
  44.     private static void printMe(List<String> list1) {
  45.         for(String b : list1){
  46.             System.out.printf("%s ", b);
  47.         }
  48.         System.out.println();
  49.     }
  50.  
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement