title: Casting from ArrayList<Object> to my own class
body: <p>I have two ArrayLists, teamList1 and teamList2, which each contain five Team objects. I'm comparing those contents to each other in one of my methods. I <strong>must</strong> pass in these two ArrayLists as a single 2-element simple array argument, Objects[], into the method. I'm getting a compiler error because I'm struggling with casting from type Objects into type Team. In other words, changing from a Collection to a simple array back to a Collection is giving me an error. Anyone have a tip on my casting error?</p>
  
  <p><strong>CommonElements.java</strong> </p>
  
  <pre><code>package test;
  
  import javax.swing.*;
  import java.util.*;
  
  public class CommonElements {
  
      List&lt;Comparable&gt; teamList1 = new ArrayList&lt;Comparable&gt;();
      List&lt;Comparable&gt; teamList2 = new ArrayList&lt;Comparable&gt;();  
      List&lt;Comparable&gt; commonList = new ArrayList&lt;Comparable&gt;();
  
      Object[] listCollection = new Object[2];   
      int comparisonCount;
  
      public static void main(String[] args) {
          new CommonElements();       
      }
  
      public CommonElements() {
  
          comparisonCount = 0;
  
          Team a = new Team("Boston");
          Team b = new Team("Seattle");
          Team c = new Team("Newark");
          Team d = new Team("Houston");
          Team e = new Team("Salt Lske City");
  
          teamList1.add(a);
          teamList1.add(b);
          teamList1.add(c);
          teamList1.add(d);
          teamList1.add(e);
  
          Team f = new Team("Seattle");
          Team g = new Team("Nashville");
          Team h = new Team("St. Louis");
          Team i = new Team("New York");
          Team j = new Team("Boston");
  
          teamList2.add(f);
          teamList2.add(g);
          teamList2.add(h);
          teamList2.add(i);
          teamList2.add(j);    
  
  
          listCollection[0] = teamList1;
          listCollection[1] = teamList2;
  
          findCommonElements(listCollection);
          System.out.println(comparisonCount);          
      }
  
      public Comparable[] findCommonElements(Object[] collections)
      {        
          ArrayList&lt;Object&gt; objectTeam1 = new ArrayList&lt;Object&gt;(Arrays.asList(collections[0]));
          ArrayList&lt;Object&gt; objectTeam2 = new ArrayList&lt;Object&gt;(Arrays.asList(collections[1]));
  
          ArrayList&lt;Team&gt; team1 = (ArrayList)objectTeam1;
          ArrayList&lt;Team&gt; team2 = (ArrayList)objectTeam2;
  
  
          Team[] commonList = new Team[5];
          int  i = 0;
          for(Team x:team1)
          {
              for(Team y:team2)
              {       
                  comparisonCount++;
                  if(x.compareTo(y) == 0)
                  { 
                      commonList[i] = x;                      
                      System.out.println(commonList[i].teamName); 
                      i++;
                      break; /*to ensure it looks for only one match per entry*/                    
                  }
              } 
          }        
          return commonList;
      }
  
      public int getComparisons()
      {
          return comparisonCount;
      }
  }
  </code></pre>
  
  <p><strong>Team.java</strong> </p>
  
  <pre><code>package test;
  
  public class Team implements Comparable&lt;Team&gt; {
  
      String teamName = new String();
  
      public void setName ( String n ) {
          teamName = n;
      } 
  
      public Team(String n) {
          setName(n);
      }
  
  
      public int compareTo(Team x)
      {
          if(this.teamName.equals(x.teamName))
          {
              return 0;
          }
          else
          {
              return -1;
          }
      }
  
  
  }
  </code></pre>