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

  • Home
  • SEARCH
  • 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 7594507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:26:01+00:00 2026-05-30T21:26:01+00:00

Really basic OO comprehension issue I am running into, any help is greatly appreciated.

  • 0

Really basic OO comprehension issue I am running into, any help is greatly appreciated.

I’m trying to add instances of “Thing” to an arraylist every-time I press a button, I can’t wrap my head around how to create unique instances to add to the list. A different button press should remove the most recent object from the list.

ArrayList myList = new ArrayList<Thing>();
if(input.isKeyPressed(Input.KEY_A)){
            Thing myThing = new Thing();
            myThing.setNumber(myList.size());
            myList.add(myThing);
        }

if(input.isKeyPressed(Input.KEY_R)){
            if(myList.size()>0){
                myList.remove(myList.size()-1);
            }
        }

If I plan on making lots of “things” and I don’t care about what they are called (nor do I want to keep track of unique thing-object names). How can I create a unique ‘thing’ object on each button press with minimal pain.

UPDATE:
Thanks for the comments, please let me try to articulate my question better…
When I create an ArrayList full of ‘Thing’, each instance of which is called “myThing”, every instance has the same instance variables values.

If I wanted some of the ‘Thing”s to have boolean isVisable = true, and other’s to have boolean isVisable = false. I get stuck because each element of the list has the same name.

  • 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-05-30T21:26:02+00:00Added an answer on May 30, 2026 at 9:26 pm

    Make sure that Thing implements equals and hashCode correctly and then store the instances in a Set collection (i.e. HashSet). With the implementation of hashCode() and equals() it will be completely up to you which two instances of Thing are the same and hence you will be able to enforce uniqueness any way you need.

    Now the trick here is that implementing hashCode() and equals() is not entirely trivial, but you need to know how to do it if you plan to use Java. So read the appropriate chapter of Effective JAva (or better yet read the entire book).

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

Sidebar

Related Questions

This is a really basic issue, but I'm new to perl and cannot work
I have a really basic question concerning unix timestamp and mysql date. I'm trying
Trying to do some really basic math here, but my lack of understanding of
Really basic question here. So I'm told that a dot . matches any character
This seems like a really basic thing that I'm doing, yet I'm tearing my
I am having difficulty with something really basic. I am trying to implement a
I'm trying to build a really basic program that will alternate between two pictures
A really basic question, I have recently started exploring Objective C and Trying to
I've got a really basic symfony site and I'm trying to display an OpenLayers
I am trying to do something really basic but can not seem to find

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.