How to check that the object isn't the last and how to handle it if it is?

KJGarbutt

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
    }
T.J. Crowder

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to check if the object passed into a method isn't a derived type?

How to check if an object is a subclass of Handle

How to check last array object

C++/CLI how do I tell if a handle isn't pointing to any object

How come localStorage isn't accesable as an object?

How to detect that raycast isn't hitting object

How to get the last index of a comboBox when size isn't constant?

Elixir - how to use a default function parameter that isn't in the last position?

In a thread of emails, how to reply to the last sender that isn't you?

How to handle the last case?

How to check if a pipe is empty and run a command on the data if it isn't?

espresso how do you check if a dialog isn't visible

How to check if directory is mounted on disc X and mount it if it isn't

How to check if the thousandth decimal place isn't 0

How to check if a value isn't changed in between <span> tag

How to check why my custom validator isn't working?

How can I check if the marker is or isn't in the inside a specific circle

Ruby: How to check whether a string isn't blank?

How to check if input is a number, and do something about it if it isn't?

How to check if value is in dictionary or why it isn't working?

How to override any operator if the first operand isn't your object

How do I access a UICollectionViewCell object that isn't yet visible?

How to register an object (that isn't a view controller) as a Notification Center observer

How to mock an object with OCMock which isn't passed as a parameter to method?

How to delete an object in the heap that isn't stored to a variable pointer?

How to resolve The operator '[]' isn't defined for the type 'Object'?

How to check the last chunk?

how to check if it was the last element

How to handle an object in java?