title: Array Index Out of Bounds For Loop Print Contents ArrayList
body: <pre><code>// ArrayList
  import java.io.*; 
  import java.util.*;
  
  public class ArrayListProgram
  {
  public static void main (String [] args)
  {
  Integer obj1 = new Integer (97);
  String obj2 = "Lama";
  CD obj3 = new CD("BlahBlah", "Justin Bieber", 25.0, 13);
  
  ArrayList objects = new ArrayList();
  
  objects.add(obj1);
  objects.add(obj2);
  objects.add(obj3);
  
  
  System.out.println("Contents of ArrayList: "+objects);
  System.out.println("Size of ArrayList: "+objects.size());
  
  BodySystems bodyobj1 = new BodySystems("endocrine");
  BodySystems bodyobj2 = new BodySystems("integumentary");
  BodySystems bodyobj3 = new BodySystems("cardiovascular");
  
  objects.add(1, bodyobj1);
  objects.add(3, bodyobj2);
  objects.add(5, bodyobj3);
  
  System.out.println();
  System.out.println();
  
  int i;
  for(i=0; i&lt;objects.size(); i++);
  {
  System.out.println(objects.get(i));
  }
  </code></pre>
  
  <p>}
  }</p>
  
  <p>The for loop is attempting to print the contents of the array list using the size() method. How can I stop getting an ArrayIndexOutOfBounds error? </p>
  
  <p>I have indexes 0-5 in my array list (6 objects).</p>
  
  <pre><code>Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 6, Size: 6
      at java.util.ArrayList.RangeCheck(ArrayList.java:547)
      at java.util.ArrayList.get(ArrayList.java:322)
      at ArrayListProgram.main(ArrayListProgram.java:37)
  </code></pre>