java - 我的循环出了什么问题?

标签 java repast-simphony

我有一个包含 6 个盒子的列表 (boxlist),每个盒子有 1 个对象 (num_objects)。每个框都存在于地理上下文中(位于特定的纬度和经度坐标):

    //iterate through each of the boxes in the list (6)
    for (int i = 0; i < boxlist.size(); i++){
        //get the first box... then second box... etc (call it target box)
        Box targetbox = boxlist.get(i);
        Context context = ContextUtils.getContext(targetbox);
        Geography<Object> geography = (Geography)context.getProjection("Geography");
        Geometry geom = geography.getGeometry(targetbox);
        //get the coordinates of the target box
        Coordinate coord = geom.getCoordinates()[0];

        //for each of the 6 boxes, get the number of objects in the target box
        double num = targetbox.getNum_objects();
        // print the number of objects in each box (1)
        System.out.println(num);

        //create random utility
        Random random = new Random();
        // create the same number of BoxObjects as the num_objects in the box and place them in the geography at the same location as the box they are in (take them out of the box)
        for (int j = 0; j < num; j++) {
            boolean randomBoolean = random.nextBoolean();
            boolean anotherBoolean = false;
            BoxObject obj = new BoxObject(context, geography, randomBoolean, anotherBoolean);
            context.add(obj);
            // move to the object to the same coordinates of its box
            geography.move(obj, new GeometryFactory().createPoint(coord));
        }   

  } 

我的循环正确地计算出有 6 个盒子,盒子中有 1 个对象,应该创建 6 个对象,但它创建了 12 个对象。我该如何解决这个问题?

注意:在实际模拟中,盒子可能有多个对象。

最佳答案

我的问题是这段代码嵌套在 Agent 类中。我有两个代理,它运行该方法两次,每个代理运行一次(产生双倍的对象)。当我将此代码移至主模型类时(因为从技术上讲,它与代理没有任何关系),它运行得很好。

关于java - 我的循环出了什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28750892/

相关文章:

java - EditText 不出现复制粘贴选项

java - 在java中构建特定大小的信封对象?

repast-simphony - 既然 Groovy 编译器 2.4 正式不可用,如何在 Eclipse 中启动 Repast Simphony?

repast-simphony - 就餐 Simphony 3D GIS

java - 使用 Spring boot 在 thymeleaf 模板中打印数据时出现问题

java - 在 HashSet 中存储坐标

java - 是否可以使这个嵌套 for 循环小于 O(N^2)?

java - 如何保存RecyclerView滚动位置?当应用程序关闭时

java - 在 java if 语句中混合关系条件和 boolean 条件