Advertisement
makispaiktis

I6. List (stable size) vs ArrayList (addAll)

Jun 6th, 2022 (edited)
1,178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.80 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class myClass {
  4.  
  5.     public static void main(String[] args){
  6.  
  7.         // Convert stuff array to a list
  8.         String[] stuff = {"apples", "beef", "corn", "ham"};
  9.         List<String> list1 = Arrays.asList(stuff);
  10.         System.out.println("List = " + list1);
  11.  
  12.         // !!!!!!!!!!!!!!!!!!!!!!!!!!!
  13.         // Converting an array into a 'List' means stable size
  14.         // For varied size, we need 'ArrayList'
  15.         // !!!!!!!!!!!!!!!!!!!!!!!!!!!
  16.  
  17.         ArrayList<String> list2 = new ArrayList<String>();
  18.         list2.add("Youtube");
  19.         list2.add("Google");
  20.         list2.add("Digg");
  21.         System.out.println("ArrayList = " + list2);
  22.         Collections.addAll(list2, stuff);
  23.         System.out.println("ArrayList with 'addAll' = " + list2);
  24.  
  25.     }
  26. }
  27.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement