Can I get some working code examples that I can pick apart and study how they work?
Specifically, I need to figure out how do I get the address field from the x numbered object in my list?
How do I delete the xth object from the list?
How do I set the price field of object x to a new value?
I understand how the houseList.add places the data into a new object that is appended to the end of the list, but I do not understand the converse of it and how to target a specific object and manipulate it. Please show a working multi-class example. Thanks!
The class that creates the House object and it holds the following information;
public House(int address, String street, double price, int rooms) {
this.address = address;
this.street = street;
this.price = price;
this.rooms = rooms;
}
and it has the following methods...
public void SetAddress (int address){
setAddress(address);
}
private void setAddress (int address){
this.address = address;
}
public int GetAddress (int address){
address = getAddress(address);
return address;
}
private int getAddress (int address){
return address;
}
@Override
public String toString() {
return address + " " + street + " " + price + " " + rooms;
}
etc...
In another class, I declare a List of House objects named houseList and that it is an ArrayList.
List<House> houseList;
houseList = new ArrayList();
and can fill it with data,
houseList.add(new House(this.address, this.street, this.price, this.rooms));
and iterate through it...
public void showHouses() {
Iterator<House> itr = houseList.iterator();
while (itr.hasNext()) {
System.out.println(itr.next().toString());
}
}
up to this point, none of my House methods as listed above come into play, except for the toString method which has been overridden. What I have not been able to figure out is how to pull this.street or this.price for the xth element of the list. I can place data into the list with
houseList.add(new House(this.address, this.street, this.price, this.rooms));
and I can see my data is stored as expected when iterated through. I have these setters and getters, but I am not understanding how they are to be used to pull these items out or set them in. Since I used houseList.add, I figured the opposite of add would be to get, but haven't figured out the correct syntax.
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());
}