Advertisement
mrScarlett

ArrayList intro

Mar 5th, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.18 KB | None | 0 0
  1. import java.util.ArrayList;
  2. public class arrayList
  3. {
  4.     public static void main (String args []){
  5.         ArrayList <Integer> myList = new ArrayList <Integer>(10);
  6.         myList.add(199);
  7.         myList.add(4);
  8.         myList.add(100);
  9.         myList.add(555);
  10.         myList.add(40);
  11.         myList.add(49);
  12.        
  13.         for (Integer x : myList){
  14.             System.out.println(x);
  15.            
  16.         }
  17.        
  18.         //size does not indicate the intitial size only the amount of items stored
  19.         System.out.println("size="+myList.size());
  20.        
  21.         myList.remove(1);
  22.        
  23.         //size does not indicate the intitial size only the amount of items stored
  24.         System.out.println("size="+myList.size());
  25.        
  26.         myList.set(1, 50);//myList.set(index, value), changes value of index in the ArrayList
  27.        
  28.         /* trims the size of the arrayList to how many items are stored
  29.          * if the initial size was 10 but there were 6 items stored the
  30.          * trimToSize method would reduce the initial size to 6.
  31.          * */
  32.         myList.trimToSize();
  33.        
  34.         myList.clear();//clears the contents of the arrayList
  35. }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement