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.

Reustonium
  • 2,575
  • 2
  • 16
  • 16
  • 3
    What is the problem with the above code? – Oliver Charlesworth Feb 26 '12 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. – tester Feb 26 '12 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. – Emil L Feb 26 '12 at 21:08
  • Looks like you want to use a Stack instead of an ArrayList. – Marcus Johansson Feb 26 '12 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`? – Reustonium Feb 26 '12 at 22:33
  • @Reustonium, yes it is a new thing. The myThing reference changes on every assignment even if it has the same name in your code. Try adding System.out.println(myThing) before the end of the if clause braces, and you'll see a different hashcode assigned to it. Also see this for more detail: http://stackoverflow.com/questions/1961146/memory-address-of-variables-in-java – Isaac Feb 26 '12 at 22:52

2 Answers2

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

MK.
  • 33,605
  • 18
  • 74
  • 111
  • 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. – Reustonium Feb 26 '12 at 23:04
  • 1
    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. – MK. Feb 27 '12 at 04:01
  • Could you recommend at follow up to 'head first java'? thanks again. – Reustonium Feb 27 '12 at 14:32
  • Sorry, don't know. Time to write and read some code probably. – MK. Feb 27 '12 at 14:55
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