问题描述:

I have defined an abstract class as follows:

public abstract class Move implements Comparable<Move> {

protected int cell;

protected int weight;

public int getWeight()

{

return this.weight;

}

public void setWeight(int value)

{

this.weight = value;

}

protected Move(int cell)

{

this.cell = cell;

this.weight = 0;

}

protected Move(int cell, int weight)

{

this.cell = cell;

this.weight = weight;

}

@Override

public int compareTo(Move m)

{

return this.weight - m.weight;

}

I have an additional 2 classes that extend this class (classed MoveLeft and MoveRight). I add both types of objects to a List of type Move and then sort using Collections.sort:

List<Move> moves = new ArrayList<Move>(someSize);

moves.add(new MoveLeft(cell1));

moves.add(new MoveRight(cell2));

moves.add(new MoveRight(cell3));

moves.add(new MoveLeft(cell4));

Collections.sort(moves);

However, the list is being sorted by cell instead of by weight.

Is it not possible to mix different subclassed instances in the same sort?

Note: I'm setting a unique value for weight in the subclassed constructors.

网友答案:

You must create array of Move populate it with mix of derived classes upcasting it to Move and sort it as usual, then you can check you actual class using isntanceOf and downcast.

网友答案:

This is really a very long comment, rather than an answer.

I wrote a simple test program, and it appears to sort correctly. The output is [Move [cell=10, weight=1], Move [cell=1, weight=100]], which is neither the order in which I added the elements, nor ascending cell order, but is ascending weight order.

I note that you have two constructor parameters of the same type. I suggest very careful checking that they are not getting switched around. If that is not the problem, I suggest trying to modify my test program to make it more similar to the real code, until it reproduces the problem. Here is my test program:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test {
  public static void main(String[] args) {
    List<Move> list = new ArrayList<Move>();
    list.add(new MoveRight(1, 100));
    list.add(new MoveLeft(10, 1));
    Collections.sort(list);
    System.out.println(list);
  }
}

abstract class Move implements Comparable<Move> {
  protected int cell;
  protected int weight;

  public int getWeight()
  {
    return this.weight;
  }

  public void setWeight(int value)
  {
    this.weight = value;
  }

  protected Move(int cell)
  {
    this.cell = cell;
    this.weight = 0;
  }

  protected Move(int cell, int weight)
  {
    this.cell = cell;
    this.weight = weight;
  }

  @Override
  public int compareTo(Move m)
  {
    return this.weight - m.weight;
  }

  @Override
  public String toString() {
    return "Move [cell=" + cell + ", weight=" + weight + "]";
  }
}

class MoveLeft extends Move {

  protected MoveLeft(int cell, int weight) {
    super(cell, weight);
  }

}

class MoveRight extends Move {

  protected MoveRight(int cell, int weight) {
    super(cell, weight);
  }

}
相关阅读:
Top