Java 将同一对象添加到上下文和列表中

Java 将同一对象添加到上下文和列表中,java,eclipse,functional-programming,repast-simphony,Java,Eclipse,Functional Programming,Repast Simphony,我创建了一个树类并实现了一个比较器: class SuitComp implements Comparator<Tree> { @Override public int compare(Tree o1, Tree o2) { return Double.compare(o1.getSuit(), o2.getSuit()); } } class Tree { private ContinuousSpace<Object> space;

我创建了一个树类并实现了一个比较器:

class SuitComp implements Comparator<Tree> {

@Override
public int compare(Tree o1, Tree o2) {
    return Double.compare(o1.getSuit(), o2.getSuit());
    }   
}

class Tree {
    private ContinuousSpace<Object> space;
    private Grid<Object> grid;
    public double suitability;
    public int id;
    public static int count = 1;

public Tree(ContinuousSpace<Object> space, Grid<Object> grid, double suitability, int id) {
    this.space = space;
    this.grid = grid;
    this.id = count;
    count++;    
}

public double getSuit() {
    return suitability;
}

public void setSuit(double suitability) {
    this.suitability = suitability;
}

@Override
public String toString() {
    return "Tree " + id + " is the most suitable, with a value of: " + suitability;
}

public void measureSuit() {
    System.out.println("Tree number " + id + " has a suitability of " + suitability);
}

我的问题是:是否可以将10个树对象添加到上下文中,并将相同的对象添加到列表中?

您是否尝试过显而易见的方法?i、 e.类似于以下内容:

        ArrayList<Tree> trees = new ArrayList<Tree>();

        int treeCount = 10;
        for (int i = 0; i < treeCount; i++) {
            double suitability = Math.random();
            int id = i;
            // We create the Tree...
            Tree tree = new Tree(space, grid, suitability, id);
            // ... then add it to the context
            context.add(tree);
            // ... then add it to the list
            trees.add(tree);
ArrayList trees=new ArrayList();
int treeCount=10;
对于(int i=0;i

我看不出它不起作用的原因。但是我没有使用RepastSimphony的经验,所以我很可能是错的:(

谢谢。有点尴尬。我以为我已经试过了。此外,这还解决了我的其他问题。
        ArrayList<Tree> trees = new ArrayList<Tree>();

        int treeCount = 10;
        for (int i = 0; i < treeCount; i++) {
            double suitability = Math.random();
            int id = i;
            context.add(new Tree(space, grid, suitability, id));

        Tree tree;

        tree = new Tree(space, grid, suitability, id);
        trees.add(tree);
        tree.measureSuit();


        Tree maxTree = Collections.max(trees, new SuitComp());
        System.out.println(maxTree);

        }


        for (Object obj : context) {
            NdPoint pt = space.getLocation(obj);
            grid.moveTo(obj, (int)pt.getX(), (int)pt.getY());

        }

        return context;

    }

}   
        ArrayList<Tree> trees = new ArrayList<Tree>();

        int treeCount = 10;
        for (int i = 0; i < treeCount; i++) {
            double suitability = Math.random();
            int id = i;
            // We create the Tree...
            Tree tree = new Tree(space, grid, suitability, id);
            // ... then add it to the context
            context.add(tree);
            // ... then add it to the list
            trees.add(tree);