title: How do you implement Comparable in this situation?
body: <p>I've looked up examples as to how Comparable works and I somewhat understand how it would work but I don't know how I would use it in this situation.</p>
  
  <p>I have an <code>ArrayObject class implements Comparable</code> and imported <code>java.util.*;</code> I also have an <code>ArrayObjectDriver</code> class that is a main method that calls upon methods I've coded in the <code>ArrayObject</code> class. One of the methods that it has to be able to do is to sort the array of objects in the main method. I know you have to use something with <code>.compareTo</code> but I'm not sure how I would do that in the <code>ArrayObject</code> class and call on it in the driver.</p>
  
  <p>EDIT: ArrayObject code</p>
  
  <pre><code>public class ArrayObject implements Comparable
  {
  private Object[] arr;
  private int actualSize;
  ArrayObject()
  {
      arr = new Object[10];
      actualSize = 0;
  }
  
  ArrayObject(int size)
  {
      arr = new Object[size];
      actualSize = 0;
  }
  
  public void add(Object obj)
  {
      if(actualSize&gt;=arr.length)
          expandArray();
      arr[actualSize]=obj;
      actualSize++;
  }
  
  public void expandArray()
  {
      int newSize = arr.length*2;
      Object[] biggerList = new Object[newSize];
      for(int i=0; i&lt;arr.length; i++)
      {
          biggerList[i] = arr[i];
      }
      arr = biggerList;
  }
  
  public void add(Object obj, int index)
  {
      if(index&lt;actualSize)
      {
          shiftRight(index);
          arr[index]=obj;
          actualSize++;
      }
      // index is between [0 and actualSize)
  }
  
  private void shiftRight(int start)
  {
      for(int i=actualSize; i&gt;start; i--)
      {
          arr[i] = arr[i-1];
      }
      arr[start]=null;
  }
  
  private void shiftLeft(int start)
  {
      for(int i=start; i&lt;actualSize-1; i++)
      {
          arr[i] = arr[i+1];
      }
      arr[actualSize-1]=null;
  }
  
  public Object remove(int index)
  {   // returning the object you are removing
      // ("the", "is", "are")
      if(index&gt;=0&amp;&amp;index&lt;actualSize)
      {
          Object obj = arr[index];
          arr[index] = null;
          // arr[index] = arr[actualSize-1];
          // what to do about the null?
          // Shift to the left by one
          shiftLeft(index);
          actualSize--;
          // ("the", null, "are")
          return obj;
      }
      return null;
  }
  
  public Object get(int index)
  {
      if(index&gt;=0&amp;&amp;index&lt;actualSize)
          return arr[index];
      return null;
  }
  
  public int sizeOfContainer()
  {
      return arr.length;
  }
  
  public int items()
  {
      return actualSize;
  }
  
  public boolean searchArray(Object obj)
  {
      for(int i=0; i&lt;actualSize; i++)
      {
          if(arr[i].equals(obj))
              return true;
      }
      return false;
  }
  
  public int findObject(Object obj)
  {
      if(searchArray(obj))
      {
          for(int i=0; i&lt;actualSize; i++)
          {
              if(arr[i].equals(obj))
                  return i;
          }
      }
      return -1;
  }
  
  public boolean isItEmpty()
  {
      if(actualSize == 0)
          return true;
      return false;
  }
  
  public int removeSearch(Object obj)
  {
      for(int i=0; i&lt;actualSize; i++)
      {
          if(arr[i].equals(obj))
          {
              remove(i);
              return i;
          }
      }
      return -1;
  }
  
  public void clearArray()
  {
      for(int i=0; i&lt;actualSize; i++)
          arr[i] = remove(i);
  }
  
  public void printArr()
  {
      System.out.println("Array Size: " + actualSize);
      for(int i=0; i&lt;actualSize; i++)
      {
          System.out.println(arr[i]);
      }
  }
  }
  </code></pre>
  
  <p>ArrayObjectDriver code</p>
  
  <pre><code>public class ArrayObjectDriver
  {
  private static Scanner scanner;
  public static void main(String[] args)
  {
      scanner = new Scanner(System.in);
      ArrayObject array = new ArrayObject();
      int selection = selectionMenu();
      while(selection &gt; 0)
      {
          if(selection == 1)
          {
              System.out.println("Enter your object: ");
              String str = scanner.next();
              array.add(str);
          }
          else if(selection == 2)
          {
              System.out.println("Enter your object: ");
              String str = scanner.next();
              System.out.println("Enter location: ");
              int n = scanner.nextInt();
              array.add(str, n);
          }
          else if(selection == 3)
          {
              System.out.println("Enter location: ");
              int n1 = scanner.nextInt();
              array.remove(n1);
          }
          else if(selection == 4)
          {
              System.out.println("Enter object: ");
              String str = scanner.next();
              int i = array.removeSearch(str);
              System.out.println("Object removed at index " + i);
          }
          else if(selection == 5)
          {
              array.clearArray();
              System.out.println("Cleared Array");
          }
          else if(selection == 6)
          {
              System.out.println("Enter object: ");
              String str = scanner.next();
              boolean result = array.searchArray(str);
              System.out.println("The object was found: " + result);
          }
          else if(selection == 7)
          {
              boolean result = array.isItEmpty();
              System.out.println("It is empty: + result);
          }
          else if(selection == 8)
          {
              array.expandArray();
              int i = array.sizeOfContainer();
              System.out.println("The new size of the array is: " + i);
          }
          else if(selection == 9)
          {
              System.out.println("Enter object: ")
              String str = scanner.next();
              int i = array.findObject(str);
              System.out.println("The object was found at index " + i);
          }
          else if(selection == 10)
              array.printArr();
          else if(selection == 11)
          {
  
          }
          else if(selection == 12)
              System.exit(0);
          System.out.println("");
          selection = selectionMenu();
      }
  }
  
  private static int selectionMenu()
  {
      System.out.println("Menu: ");
      System.out.println("1. Add object to the end of the list");
      System.out.println("2. Add object at a specific location");
      System.out.println("3. Remove specific object at a location");
      System.out.println("4. Remove specific object that matches name");
      System.out.println("5. Empty the array");
      System.out.println("6. See if the array contains a certain object");
      System.out.println("7. See if the array is empty"); 
      System.out.println("8. Expand your array");
      System.out.println("9. Search for an item");
      System.out.println("10. Print array");
      System.out.println("11. Sort array");
      System.out.println("12. Exit");
      System.out.println("Enter option: ");
      int optionNumber = scanner.nextInt();
      return optionNumber;
  }
  }
  </code></pre>