2

Hope you don't mind I'm asking a newbie question about the use of iterator for a arrayList / List.

I have a Building Class with a few data members:

public Building() {
    private String buildingName;
    private String buildingType;
    private double width;
    private double length;
    private String managerName;
    ....
}

I've already set toString() as follow to access the name String and I'm NOT allowed to change toString to return a different data member.

public String toString() {
    return buildingName;
}

I've created a bunch of building objects in the Building class by loading a building.xml file, and I've used iterator to create an array/list of buildingNames to be viewed in a swing ComboBox (but let's ignore those swing component below and focus on the just the logics):

// Create building combo box model.
buildingComboBoxModel = new DefaultComboBoxModel<Building>();
buildingsCache = new ArrayList<Building>(buildings);
Iterator<Building> i = buildingsCache.iterator();
while (i.hasNext()) buildingComboBoxModel.addElement(i.next());

// Create building list.
buildingComboBox = new JComboBoxMW<Building>(buildingComboBoxModel);
buildingComboBox.addActionListener(this);
buildingComboBox.setMaximumRowCount(10);
buildingSelectPanel.add(buildingComboBox);

How many ways do can you think of and how in putting together an list of buildingType using iterator without significant code changes?

If I need to modify some of the buildingType Strings in some situations by adding a String to it,

say in one building,

String buildingName = "Adam's Place";
String buildingType = "Greenhouse";

[post edit]

e.g. I need to change buildingType to "Greenhouse Type 1" for "Adam's Place" and change another building's buildingType to "Greenhouse Type 2"

Can it be done using iterator and how?

If not, other ways are welcome

Thanks in advance!

2
  • Where is the 205 coming from? Commented Oct 28, 2014 at 23:02
  • I just edited my questions. What I mean is that I need to make the buildingType a little more specific than what came out of the xml file. Commented Oct 28, 2014 at 23:08

1 Answer 1

1

There is no special way for handling objects read from an iterator. If the only changes needed are just setting different values to the members of the Building class, this can be done the standard way, i.e. by adding member setter functions.

For example, to modify the buildingType at any time, a setBuildingType() method is needed:

public class Building {
    private String buildingName;
    private String buildingType;
    private double width;
    private double length;
    private String managerName;
    ....
    public void setBuildingType(String buildingType) {
        this.buildingType = buildingType;
    }
}

Given the above, the iterator-based code can be modified as follows:

Iterator<Building> i = buildingsCache.iterator();
while (i.hasNext()) {
    Building b = i.next();
    b.setBuildingType("Whatever type");
    buildingComboBoxModel.addElement(b);
}
Sign up to request clarification or add additional context in comments.

3 Comments

okay b.setBuildingType("Whatever type") will enable me to change the buildingType to whatever I want. But how do I create a list of all buildingTypes (ok to have duplicates for now) from the Building objects in the first place to make the list show up on the ComboBox before I go about modifying certain buildingTypes?
if I do this: while (i.hasNext()) { Building b = i.next(); buildingComboBoxModel.addElement(b.getNickName()); } , this won't work since b.getNickName() is a String, not a Building object.
Sounds like you need to put the building type in the label of the combo box. Check the corresponding Java Tutorial pages: docs.oracle.com/javase/tutorial/uiswing/components/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.