我有一种方法可以遍历一袋几何对象,并获取特定的属性,并将当前属性与前一个属性(如果有)进行比较,以存储最高属性,然后将其分配给另一个几何。 / p>
我刚刚意识到在某个时候我将到达那个Bag的末端,我需要我的方法来提醒我它已经到达了bag中的最后一个对象。
如何识别当前对象是袋子中的最后一个?
int getLargestUnassignedWard() {
Bag lsoaGeoms = centroidsLayer.getGeometries();
System.out.println();
System.out.println("Getting Largest Unassigned Wards!");
int highestOSVI = -1;
MasonGeometry myCopy = null;
for (Object o : lsoaGeoms) {
MasonGeometry masonGeometry = (MasonGeometry) o;
int id = masonGeometry.getIntegerAttribute("ID");
String lsoaID = masonGeometry.getStringAttribute("LSOA_NAME");
int tempOSVI = masonGeometry.getIntegerAttribute("L_GL_OSVI_");
Point highestWard = masonGeometry.geometry.getCentroid();
System.out.println(lsoaID + " - OSVI rating: " + tempOSVI + ", ID: " + id);
if (assignedWards.contains(id))
continue;
// tempOSVI = the attribute in the "L_GL_OSVI_" column - ints
if (tempOSVI > highestOSVI) { // if temp is higher than highest
highestOSVI = tempOSVI; // update highest to temp
myCopy = masonGeometry; // update myCopy, which is a POLYGON
}
}
if (myCopy == null) {
System.out.println("ALERT: LSOA Baselayer is null!");
return -1; // no ID to find if myCopy is null, so just return a fake value
}
int id = myCopy.getIntegerAttribute("ID"); // Here, id changes to the highestOSVI
assignedWards.add(id); // add ID to the "assignedWards" ArrayList
System.out.println("Highest OSVI Raiting is: " + myCopy.getIntegerAttribute("L_GL_OSVI_") + " for LSOA ID: "
+ id + " (" + myCopy.getStringAttribute("LSOA_NAME") + ")");
System.out.println("Current list of Largest Unassigned Wards: " + assignedWards); // Prints out: the ID for the highestOSVI
System.out.println();
return myCopy.getIntegerAttribute("ROAD_ID"); // return Road_ID for the chosen LSOA to visit
}
答案 0 :(得分:3)
我刚刚意识到在某个时候我将到达那个Bag的末端,我需要我的方法来提醒我它已经到达了bag中的最后一个对象。
我看不出有什么理由需要知道要执行该循环所要执行的操作。
但是,如果您确实需要知道循环中的内容,则不能使用增强的for
循环;您需要直接使用迭代器:
for (Iterator it = lsoaGeoms.iterator(); it.hasNext(); ) {
MasonGeometry masonGeometry = (MasonGeometry)it.next();
boolean isLast = !it.hasNext();
// ...
}
侧面说明:我强烈建议使用类型参数,而不要使用原始类型和类型转换。例如,lsoaGeoms
将是Bag<MasonGeometry>
,等等。