Добавление одного и того же объекта в контекст и список

Я создал класс дерева и реализовал компаратор:

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:

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 новых отдельных объектов дерева и добавил их в 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 объектов дерева в контекст и добавить те же объекты в список?


person Taylor Marie    schedule 03.01.2015    source источник


Ответы (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, поэтому я вполне могу ошибаться :(

person Diego    schedule 03.01.2015
comment
благодарю вас. Немного смущает. Я думал, что пробовал это. Кроме того, это решило другие мои проблемы. - person Taylor Marie; 03.01.2015