简体   繁体   English

将相同的对象添加到上下文和列表

[英]Adding the same object to context and a list

I have created a tree class and implemented a comparator: 我创建了一个树类并实现了一个比较器:

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);
}

} }

And then I have put them into a context (in geographic space on a grid ) using Respast Simphony: 然后,我使用Respast Simphony将它们放入上下文中(在网格中的地理空间中 ):

public class TreeBuilder implements ContextBuilder<Object> {

    @Override
    public Context build(Context<Object> context) {
        context.setId("taylor");
        ContinuousSpaceFactory spaceFactory = 
        ContinuousSpaceFactoryFinder.createContinuousSpaceFactory(null);
        ContinuousSpace<Object> space = 
        spaceFactory.createContinuousSpace("space", context, 
                new RandomCartesianAdder<Object>(), 
                new repast.simphony.space.continuous.WrapAroundBorders(), 
                50, 50);


        GridFactory gridFactory = GridFactoryFinder.createGridFactory(null);
        Grid<Object> grid = gridFactory.createGrid("grid", context, 
                new GridBuilderParameters<Object>(new WrapAroundBorders(), 
                new SimpleGridAdder<Object>(), 
                true, 50, 50));

If I am correct, in the code below, I created 10 tree objects, added them to the context and then created 10 new separate tree objects and adding them to the ArrayList: 如果我是正确的,那么在下面的代码中,我创建了10个树对象,将它们添加到上下文中,然后创建了10个新的单独的树对象,并将它们添加到ArrayList中:

        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);

Next I print the suitability values and the maximum suitability value. 接下来,我打印适用性值和最大适用性值。

        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;

    }

}   

My question is: Is it possible to add the 10 tree objects to the context and add the same objects to the list? 我的问题是:是否可以将10个树对象添加到上下文并将相同的对象添加到列表?

Have you tried the obvious? 你尝试过显而易见的吗? ie something like the following: 即类似以下内容:

        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);

I don't see a reason why it wouldn't work. 我看不出它为什么不起作用的原因。 But I have no experience with RepastSimphony so I could very well be wrong :( 但是我没有RepastSimphony的经验,所以我很可能是错的:(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM