title: Java printing an array of objects
body: <p>I know there are a lot of pages about this question, but I cannot understand it in my case. </p>
  
  <p>I need to print the array of objects. For example, I have an array of objects that hold objects from the "shape" class. Do I call the toString method for each object in the array, or do I code the toString method in ObjectList to print out the instance variables? If so, how do I do that?</p>
  
  <pre><code>public class Shape{
      private String shapeName;
      private int numSides;
  
      public String toString(){
          return shapeName + " has " + numSides + " sides.";
      }
  }
  
  public class ObjectList{
      private Object[] list = new Object[10];
      private int numElement = 0;
  
      public void add(Object next){
          list[numElement] = next;
      }
  
      public String toString(){
          // prints out the array of objects 
  
          // do I call the toString() method from the object?
  
          // or do I print the instance variables? What am I printing?
  
          // I'm guessing I do a for loop here
      }
  }
  
  public class Driver{
      public static void main(String[] args){
          ObjectList list = new ObjectList();
          Shape square = new Shape("square", 4);
          Shape hex = new Shape("hexagon", 6);
          list.add(square);
          list.toString();  // prints out array of objects
  }
  </code></pre>
  
  <p>I am aiming for it to print this:</p>
  
  <pre><code>square has 4 sides
  hexagon has 6 sides
  </code></pre>