【问题标题】:Adding the same object to context and a list将相同的对象添加到上下文和列表
【发布时间】:2015-01-03 01:48:09
【问题描述】:

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

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

}

然后我使用 Respast Simphony 将它们放入一个上下文中(在 grid 上的地理 空间 中):

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

如果我是正确的,在下面的代码中,我创建了 10 个树对象,将它们添加到上下文中,然后创建了 10 个新的 separate 树对象并将它们添加到 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);

接下来我打印适宜性值和最大适宜性值。

        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;

    }

}   

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

【问题讨论】:

    标签: java eclipse functional-programming repast-simphony


    【解决方案1】:

    你尝试过显而易见的事情吗?即类似于以下内容:

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

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

    【讨论】:

    • 谢谢。有点尴尬。我以为我已经尝试过了。此外,这解决了我的其他问题。
    猜你喜欢
    • 1970-01-01
    • 2019-01-27
    • 2019-10-08
    • 1970-01-01
    • 2023-03-26
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多