Results 1 to 9 of 9
  1. #1
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default A question about using an iterator

    I want to cycle through all the elements in my ArrayList to find the element with the highest value of a particular variable. I had thought that using an iterator was the best way to do this, but I'm getting stuck. I have a class like this:

    Java Code:
    public class Power_Stations {
        public int Capacity;
        public int Fuel_Req;
        public int Base_Cost;
        public Parameters.Fuel_Type Type_Fuel;
        public Parameters.playerColour Ownership;
    }
    And an ArrayList like so:

    Java Code:
    public static ArrayList<Power_Stations> plantMarket = new ArrayList<Power_Stations>();
    I had planned to use something like this to find the element with the highest Base_Cost, but can't figure it out.

    Java Code:
    Iterator marketIterator = plantMarket.iterator();
            int maxCost=0;
            while(marketIterator.hasNext()) {   //how do i access the base_cost variable from here?
                marketIterator.next();
            }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,321
    Rep Power
    25

    Default

    Have you looked at the doc for Iterator? What does the next() method return? Is that the object with the Base_Cost variable?

  3. #3
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    Yes I did read that. And yes I know it returns the object. But I don't want the object. I want the base_cost of that object. And yes I am stupid.

  4. #4
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    [you should work on your code style. Underscores are a no-no. public class variables are a no-no. Variables names starting with a Capital Case letter are a no-no.]

    old approach: cast

    Java Code:
    ArrayList list = new ArrayList();
    list.add (new Power_Stations());
    Iterator it = list.iterator();
    Power_Stations ps = (Power_Stations)it.next();

    new (and better) approach: use generics


    Java Code:
    // you're saying by this that list will onyl store Power_Stations objects.
    ArrayList<Power_Stations> list = new ArrayList<Power_Stations>();
    list.add (new Power_Stations());
    Iterator it = list.iterator();
    // no need to cast
    Power_Stations ps = it.next();

  5. #5
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    Thank you. A few follow up questions:

    1) What's wrong with public class variables? I made them public so that I could access them from other classes.

    2) Doesn't the code:

    Java Code:
    Power_Stations ps = it.next();
    create a new Power_Stations object? I don't want to create a new object, I just want to access some information about an existing object stored in an ArrayList.

  6. #6
    andy16 is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    I personally find it easier to use a while loop rather than an iterator. Try something like the below (assuming you want to get the highest capacity). Forgive me if I made a syntax error - it's been a while since I last used ArrayList.

    Java Code:
    int highest = 0; // The highest Integer value so far
    int index = 0; // The current index we are checking in the ArrayList
    int rememberIndex = 0; // Remembering the position of the highest value
    Power_Stations ps = null; // To avoid building and destroying an object for each iteration
    
    while (index < plantMarket.size()) { // Loops through all the items in your ArrayList
         ps = plantMarket.get(index);
    
         if (ps.getCapacity() > highest) { // The current object has higher capacity than the highest we have found so far
              highest = ps.getCapacity(); // Store the "new highest" capacity
              rememberIndex = index; // Remember the index (position) for the highest capacity so we can find it after looping through all of our objects
         }
    
         index++;
    }
    
    // Loop done, now return the object with the highest capacity by using our rememberIndex variable
    ps = plantMarket.get(rememberIndex);
    And just a few heads up; you should avoid using underscore in class names (and avoid using plural class names) and instead of using public data fields, use private. To access the private variables from another object, you should implement get method (formally called an accessor method) - and also set methods (mutator methods). Most IDEs will let you do this automatically, but below is how to do so manually:

    Java Code:
    // Get / accessor method for the capacity (you should begin your variables with a lower case character by the way).
    //This is the method I used in my example above and can be called on objects of class Power_Stations.
    
    public int getCapacity() {
         return capacity;
    }
    
    // Set / mutator method
    
    public void setCapacity(int capacity) {
         this.capacity = capacity; // Sets the capacity on the object to the one supplied in the parameter.
    // Alternatively, you could use a different name in the parameter and do: capacity = parameterName;
    }
    Do the above for all of your variables.
    Last edited by andy16; 05-21-2010 at 07:31 PM.

  7. #7
    Join Date
    May 2010
    Posts
    24
    Rep Power
    0

    Default

    Thanks Andy, very helpful.

  8. #8
    andy16 is offline Member
    Join Date
    May 2010
    Posts
    11
    Rep Power
    0

    Default

    You're welcome. Sorry, I did not see that you did not want the object (and base_cost instead (renamed in my example)) before now. In this case, the solution below is more efficient.

    Java Code:
    int highest = 0;
    
    for (Power_Stations cur : plantMarket) { // Loop through all the Power_Stations objects in the ArrayList "plantMarket"
    // and access the current object with the name "cur"
        if (cur.getBaseCost() > highest) // If the object we are currently checking has a higher base cost
            highest = cur.getBaseCost(); // Then store it in the variable "highest"
    }
    The above should get you the highest base cost in your list. Implement the following "get method" to get access to the private variable baseCost:

    Java Code:
    public int getBaseCost() {
        return baseCost;
    }

  9. #9
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default

    Quote Originally Posted by TaxpayersMoney View Post
    Thank you. A few follow up questions:

    1) What's wrong with public class variables? I made them public so that I could access them from other classes.

    2) Doesn't the code:

    Java Code:
    Power_Stations ps = it.next();
    create a new Power_Stations object? I don't want to create a new object, I just want to access some information about an existing object stored in an ArrayList.
    This is my understanding...

    1. Public class variables take away from the Object Oriented Design of a program. It is important to encapsulate the information inside the object, basically making all the variables private/protected then use methods to set/get the variables as needed.

    2. No it will return the next Power_Station held by the iterator. So if you create the iterator from a List plantMaker, the first call to it.next() would be the same as plantMaker.get(0), the second call would be the same as plantMaker.get(1), etc...

Similar Threads

  1. Replies: 0
    Last Post: 05-18-2010, 12:53 AM
  2. Iterator help
    By alpdog14 in forum New To Java
    Replies: 2
    Last Post: 10-13-2009, 08:42 PM
  3. iterator
    By venkatallu in forum Advanced Java
    Replies: 3
    Last Post: 09-23-2008, 01:32 PM
  4. Iterator
    By eva in forum New To Java
    Replies: 0
    Last Post: 01-31-2008, 02:07 PM
  5. How to use an Iterator in java
    By lenny in forum New To Java
    Replies: 2
    Last Post: 07-25-2007, 07:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •