无法将Comparable强制转换为实现它的类

时间:2019-02-16 12:10:03

标签: java casting genetics

我正在尝试创建班级Team的遗传ArrayList,但是尽管T扩展了Comparable,但我无法将Comparable转换为T。

(我尝试在不放入的情况下扩展Comparable,并且发生了相同的问题)

public class ArrayList<T extends Comparable>
{
    static int MaxSize = 1003;//some random maximum size for our array lists
    public int Size = 0;
    public int Capacity = 0;
    T[] MyList;
    public ArrayList()//Default Constructor
    {
        this(MaxSize);
    }
    public ArrayList(int Capacity)
    {

        MyList = (T[]) new Comparable[Capacity]; // Casting
    }
}
public class Team implements Comparable<Team>
{
    public String Name;
    public int Points;
    public int GamesCount;
    public int Wins;
    public int Loses;
    public int GoalDifference;//Goals Scored - Goals Against
    public int GoalsScored;
    public int GoalsAgainst;
    public Team(String s)
    {
      Name = s;
      Points = 0;
      GamesCount = 0;
      Wins = Loses = 0;
      GoalDifference = GoalsAgainst = GoalsScored = 0;
    }
}
public class Test
{
 public static void main(String args[])
  {
     ArrayList<Team> Teams = new ArrayList<Team>(10);
            for(int i = 0 ;i < 10;i++)
            {
                String TeamName = in.next();
                Teams.MyList[i] = new Team(TeamName);//exception here
            }
  }
}

我收到以下异常。预先非常感谢您的帮助。

线程“主”中的异常java.lang.ClassCastException:[Ljava.lang.Comparable;无法转换为[LTeam;     在Test.main(Test.java:21)

3 个答案:

答案 0 :(得分:1)

很明显,想一想。

new Comparable[Capacity];

这里您正在创建一个array,其中将包含Comparable个元素。
然后,您尝试将其下变频array中的Team,这意味着尝试使用更具体的类型,并且不允许使用该类型(Upcasting and downcasting in Java ),并且您会看到抛出ClassCastException

由于您的array仅包含extends Comparable类型,因此您在Comparable实现中可以有一个array ArrayList

Comparable[] MyList;

然后您可以初始化它而无需强制转换

MyList = new Comparable[Capacity];

请记住在Comparable#compareTo类中实现Team方法,否则代码将无法编译。


您要求一个示例。

public class ArrayList<T extends Comparable> {
    private static final int MAX_SIZE = 1000;
    private final Comparable<T>[] list;

    public ArrayList() {
        this(MAX_SIZE);
    }

    public ArrayList(int capacity) {
        list = new Comparable[capacity]; // Casting
    }

    public T get(final int index) {
        return (T) list[index];
    }

    public void set(final int index, final T value) {
        list[index] = value;
    }
}

用法非常简单

final ArrayList<Team> teamArrayList = new ArrayList<>(3);
teamArrayList.set(0, new Team("One"));
teamArrayList.set(1, new Team("Two"));
teamArrayList.set(2, new Team("Three"));

final String name = teamArrayList.get(0).Name;

扩展Comparable<T>接口时,您需要实现它

  

将此对象与指定对象进行比较。返回一个   负整数,零或正整数,因为此对象较小   大于,等于或大于指定的对象

一个基本的例子是

@Override
public int compareTo(final Team other) {
    return name.compareTo(other.name);
}

答案 1 :(得分:0)

在您的代码中,TTeam

Team IS-A Comparable。因此,您可以将Team投射到Comparable

但是每个Comparable都不是Team。因此,无法将Comparable强制转换为Team

答案 2 :(得分:0)

观看此语句:MyList = (T[]) new Comparable[Capacity];从Comparable接口初始化一个数组,并将其放入类型为Team[]的MyList字段中。 您可以使用以下方法进行测试:

{
    MyList = (T[]) new Comparable[Capacity]; // Casting
    System.out.println( MyList.getClass().toString());
}

它在输出中写入class [Ljava.lang.Comparable; ...因此,即使是在测试方法中调用length,从类外部访问此字段的第一次尝试也是如此:

public class Test
{
    public static void main(String args[])
    {
        MArrayList<Team> Teams = new MArrayList<Team>(10);
        int length = Teams.MyList.length; // this line throws ClassCastException 
        ....
    }
}

要解决您的问题,我建议像这样更改ArrayList类:

public class ArrayList<T extends Comparable<T>>
{
    ...
    Comparable<T>[] MyList;
    ...
    public ArrayList(int Capacity)
    {
        MyList = new Comparable[Capacity]; // Casting
    }
}