How to pull a field from an object stored in an arraylist?

做~自己de王妃 提交于 2019-12-04 18:41:44

Fixing getters

First, deal with the getters/setters. The following getter:

private int getAddress (int address){
    return address;
}

should be fixed to look like this:

private int getAddress() {
    return address;
}

In your code, your getAddress(int address) method has an int parameter, and returns the argument directly. It doesn't even look at the field storing the address value.

Iterating over the list

You can rewrite this:

public void showHouses() {
    Iterator<House> itr = houseList.iterator();
    while (itr.hasNext()) {
        System.out.println(itr.next().toString());
    }
}

in a more compact way, using a Java-5 style for loop, like this:

public void showHouses() {
    for (House house : houseList) {
        System.out.println(house);
        // or, say: System.out.println(house.getAddress());
    }
}

or like this, if you really want to use an iterator:

public void showHouses() {
    Iterator<House> itr = houseList.iterator();
    while (itr.hasNext()) {
        House house = itr.next();
        System.out.println(house);
        // or, say: System.out.println(house.getAddress());
    }
}

Note that in the example above, you typically need to be careful to call next() only once per iteration. That is: assign itr.next() to a local variable, and use that, instead of calling itr.next() more than once per iteration.

The advantage of the first way is that it's shorter; and it's clear that all you're doing is iterating over the collection, and not modifying it in any way (although you can still modify its values).

The advantage of the second way is that you can call itr.remove() to remove elements from the collection as you traverse (iterate over) it.

The x'th house

If you need the x'th house, you can write:

int x = ...;
House h = houseList.get(x);
System.out.println(h);
// or: System.out.println(h.getAddress());

.

Edit:

Here's one more pattern you sometimes see in code that traverses a list:

for (Iterator<House> it = houseList.iterator(); it.hasNext();) {
    House house = it.next();
    // Blah, blah..
}

This has the same advantages as the iterator-based traversal above. However, this version limits the scope of the iterator variable (it) to the loop body. In the version above, the scope of the iterator variable is the whole method body. Hence, it's easier to see that you never use the iterator outside the loop.

Limiting the scope to the loop body is also useful when you want to perform more than one iterator-based traversal in a method (or other block). It also allows you to re-use the same variable name for the second loop (even if the second loop iterates over a collection of a different type).

itr.next() returns next available House object from list. You need to call methods on that house object.

Example:

 while (itr.hasNext()) {

        House hs = itr.next();
        System.out.println(hs.getAddress(input));
        System.out.println(hs.toString());
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!