1

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.

6
  • 3
    What is the problem with the above code? Commented Feb 26, 2012 at 21:03
  • 1
    I don't get it, too. What exactly is your problem? If you want it unique, then maybe try the hash method? Is this what you want? I am not sure, sry. Commented Feb 26, 2012 at 21:04
  • 2
    You are creating a unique instance of Thing everytime you do new Thing(). You need to clarify your question a bit to illustrate what your problem is. Commented Feb 26, 2012 at 21:08
  • Looks like you want to use a Stack instead of an ArrayList. Commented Feb 26, 2012 at 21:14
  • I'll have to look into 'hash' and 'stack', I am admiditly very new to Java. Emil, when you say I create a unique instance of Thing every time I instantiate new Thing(), is it really a unique instance even though it always has the same name? myThing? Commented Feb 26, 2012 at 22:33

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the answer and the link, while I've read and understood "Head First Java", my comprehension is certainly less than total. Time for more studying.
Well, if all your Java experience at this point is "Head First Java", then "Effective Java" might be a little premature. It's a very serious text which will require you to understand a lot of hard core details about the language.
Could you recommend at follow up to 'head first java'? thanks again.
Sorry, don't know. Time to write and read some code probably.
0

try this:

$ cat Thing.java
import java.util.*;
public class Thing{
        UUID id;
        Thing () {
                id = UUID.randomUUID();

        }

        public String toString(){
                return id.toString();

        }

        public static void main(String[] argv) {
                Thing t = new Thing();

                System.out.println(t);
        }
}


$ javac Thing.java  && java Thing 
08bb3702-84d3-4bc3-b8ab-bb52b90b8f78

Comments

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.