Advertisement
Kl43z

Java ArrayList

Apr 28th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.75 KB | None | 0 0
  1. // Collection classes were created to make it easy to keep track
  2. // of groups of objects. An ArrayList differs from an array in
  3. // that it automatically resizes itself automatically. ArrayLists
  4. // are easy to add to and delete from.
  5.  
  6. import java.util.ArrayList; // The ArrayList library
  7. import java.util.Iterator; // The Iterator Library
  8. import java.util.Arrays; // The Arrays Library
  9.  
  10. public class LessonEleven {
  11.    
  12.     public static void main(String[] args)
  13.     {
  14.        
  15.         // You can create an ArrayList variable
  16.         ArrayList arrayListOne;
  17.        
  18.         // Then create an ArrayList object
  19.         // You don't have to declare the ArrayList size like you
  20.         // do with arrays (Default Size of 10)
  21.         arrayListOne = new ArrayList();
  22.        
  23.         // You can create the ArrayList on one line
  24.        
  25.         ArrayList arrayListTwo = new ArrayList();
  26.        
  27.         // You can also define the type of elements the ArrayList
  28.         // will hold
  29.         ArrayList<String> names = new ArrayList<String>();
  30.        
  31.         // This is how you add elements to an ArrayList
  32.         names.add("John Smith");
  33.         names.add("Mohamed Alami");
  34.         names.add("Oliver Miller");
  35.        
  36.         // You can also add an element in a specific position
  37.         names.add(2, "Jack Ryan");
  38.        
  39.         // You retrieve values in an ArrayList with get
  40.         // arrayListName.size() returns the size of the ArrayList
  41.         for( int i = 0; i < names.size(); i++)
  42.         {
  43.             System.out.println(names.get(i));
  44.         }
  45.        
  46.         // You can replace a value using the set method
  47.         names.set(0, "John Adams");
  48.        
  49.         // You can remove an item with remove
  50.         names.remove(3);
  51.        
  52.         // You can also remove the first and second item with
  53.         // the removeRange method
  54.         // names.removeRange(0, 1);
  55.        
  56.         // When you print out the ArrayList itself the toString
  57.         // method is called
  58.         System.out.println(names);
  59.        
  60.         // You can also use the enhanced for with an ArrayList
  61.         for(String i : names)
  62.         {
  63.             System.out.println(i);
  64.         }
  65.         System.out.println(); // Creates a newline
  66.        
  67.         // Before the enhanced for you had to use an iterator
  68.         // to print out values in an ArrayList
  69.        
  70.         // Creates an iterator object with methods that allow
  71.         // you to iterate through the values in the ArrayList
  72.         Iterator indivItems = names.iterator();
  73.        
  74.         // When hasNext is called it returns true or false
  75.         // depending on whether there are more items in the list
  76.        
  77.         while(indivItems.hasNext())
  78.         {
  79.             // next retrieves the next item in the ArrayList
  80.             System.out.println(indivItems.next());
  81.            
  82.         }
  83.        
  84.         // I create an ArrayList without stating the type of values
  85.         // it contains (Default is Object)
  86.         ArrayList nameCopy = new ArrayList();
  87.         ArrayList nameBackup = new ArrayList();
  88.        
  89.         // addAll adds everything in one ArrayList to another
  90.         nameCopy.addAll(names);
  91.         System.out.println(nameCopy);
  92.        
  93.         String paulYoung = "Paul Young";
  94.        
  95.         // You can add variable values to an ArrayList
  96.         names.add(paulYoung);
  97.        
  98.         // contains returns a boolean value based off of whether
  99.         // the ArrayList contains the specified object
  100.        
  101.         if(names.contains(paulYoung))
  102.         {
  103.             System.out.println("Paul is here");
  104.         }
  105.        
  106.         // containsAll checks if everything in one ArrayList is in
  107.         // another ArrayList
  108.         if(names.containsAll(nameCopy))
  109.         {
  110.             System.out.println("Everything in nameCopy is in names");
  111.         }
  112.        
  113.         // Clear deletes everything in the ArrayList
  114.         names.clear();
  115.        
  116.         // isEmpty returns a boolean value based on if the ArrayList
  117.         // is empty
  118.         if (names.isEmpty())
  119.         {
  120.            
  121.             System.out.println("The ArrayList is empty");
  122.            
  123.         }
  124.        
  125.         Object[] moreNames = new Object[4];
  126.        
  127.         // toArray converts the ArrayList into an array of objects
  128.         moreNames = nameCopy.toArray();
  129.        
  130.         // toString converts items in the array into a String
  131.         System.out.println(Arrays.toString(moreNames));
  132.        
  133.        
  134.     }
  135.    
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement