Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8847951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:18:45+00:00 2026-06-14T12:18:45+00:00

Can I get some working code examples that I can pick apart and study

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:18:46+00:00Added an answer on June 14, 2026 at 12:18 pm

    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).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I get some part of string that I need? accountid=xxxxxx type=prem servertime=1256876305
So I'm working on some code (for x86) where I need to get the
I've got a project I'm working on and for some reason, I can't get
I am working on some CRM 2011 Online customisations and I need to get
I am getting seriously frustrated that I can't get this to work. I've tried
Im new to programming and hopefully can get some guidance, I searched for clues
Is there a way in which I can get some information about the mounts
I am curious if I can get some help with Open Graph since I
this is a bit of a newbie question but hoping I can get some
how can i get some part of string (in symbols). For example: $string =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.