-1

I have a method that cycles through a Bag of geometry objects and gets specific attributes and compares the current attribute to the previous, if there is one, with the intention of storing the highest attribute and then assigning it to another geometry.

I've just realised that at some point I am going to reach the end of that Bag and I need my method to alert me that it has reached the last object in the bag.

How can I identify that the current object is the last in the 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
    }
KJGarbutt
  • 161
  • 8
  • 1
    Why do you need to know which is the last object? – Boris the Spider Dec 17 '18 at 10:48
  • Iterators have a `hasNext()` method – jhamon Dec 17 '18 at 10:50
  • If you use the bag's iterator directly, rather than the syntactic sugar `for` loop, you'll be able to test `hasNext()` as many times as you like within the loop. – RealSkeptic Dec 17 '18 at 10:51
  • Anyway, why are you using raw types and not `Bag`? – RealSkeptic Dec 17 '18 at 10:54
  • 1
    Bags are generally unordered (and you have provided no insight into how your bag is defined or implemented). In that sense, the "last" element is implementation-dependent, and should not be relied on - it may change from execution to execution, even with the same items in the bag. – tucuxi Dec 17 '18 at 12:22

1 Answers1

3

I've just realised that at some point I am going to reach the end of that Bag and I need my method to alert me that it has reached the last object in the bag.

I don't see any reason you need to know that to do what you've said that loop does.

But if you do need to know that within the loop, you can't use the enhanced for loop; you'll need to use the iterator directly:

for (Iterator it = lsoaGeoms.iterator(); it.hasNext(); ) {
    MasonGeometry masonGeometry = (MasonGeometry)it.next();
    boolean isLast = !it.hasNext();
    // ...
}

Side note: I strongly recommend using type parameters rather than raw types and typecasts. For instance, lsoaGeoms would be a Bag<MasonGeometry>, etc.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875