Results 1 to 5 of 5
  1. #1
    maz09 is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default Hashset and Custom Objects problem! Wont add! :(

    Hi,

    I am trying to add custom objects to a Hash Set. However, every time I try to print out the elements added, it only shows the last element as having been added :S

    I have two problems:
    1. Custom objects wont add properly to my Hash Set
    2. How do I access each object from the Hash Set to apply methods from the same class to them?
    3. Please help me as to how I can write a test class to construct an Automaton and put states into them.

    below is my code:

    Java Code:
    Machine Class:
    
    public static HashSet<State> states;
    
    public Machine(Set states, State initialState){
                    this.states = new HashSet<State>;
    		this.initialState = initialState;
    		this.currentState = initialState;
    		
    			
    	}
    Java Code:
    State Class:
    
    private static int name;
    
    public State(int name){
    		
    		this.name = name;
    	
    }
    
    public static void createStates(int number){
    		
    	Automata.states = new HashSet<State>(number);
    
          for (int i = 0; i<number;i++){
    
    	State s = new State(i);
    				
             Automata.states.add(s);
          }
    }
    So for example, if I called createStates(10) method, and tried to print out those values stored inside the hashSet, I would get 9 9 9 9 9 9 9 9 9 9 , instead of the desired 0 1 2 3 4 5 6 7 8 9
    Last edited by maz09; 03-20-2010 at 02:14 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Your code doesn't compile and is somewhat confusing to me, though it could be me. Suggestions:

    1) Post compilable code if possible.
    2) Does your State class override the hashCode() method and if so, how does it do it?
    3) Be reluctant to use static anything here other than the main method.

    Much luck!

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    And here's a BIG problem:
    Java Code:
      private [color="red"]static[/color] int name;
    Since name is static, it will be the same for all State objects. Solution: don't make it static.

    Again, avoid using static whenever possible, and only use the static modifier when you have a good reason to use it (you don't here).

  4. #4
    maz09 is offline Member
    Join Date
    Jan 2010
    Posts
    13
    Rep Power
    0

    Default

    thanks static was the reason why states were called the same.

    It adds fine now thanks :)

    However... ive added them to a LinkedHashSet so that the order in which they are inserted are preserved.

    How would I retrieve say state 4 as an object of type State from the LinkedHashSet and apply a certain method to it from the State class?

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    You could iterate through the Set to get the object you want, but perhaps you want to place them in a Map<Integer, State>, if you want to extract the State object based on its name.

Similar Threads

  1. Creating custom objects
    By coltragon in forum New To Java
    Replies: 11
    Last Post: 12-29-2009, 10:50 PM
  2. problem related to custom tags in jsp
    By garima in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 11-20-2009, 12:04 PM
  3. custom midlet-servlet chat app problem
    By vukdavid in forum CLDC and MIDP
    Replies: 0
    Last Post: 09-22-2009, 12:30 PM
  4. HashSet Contains problem
    By guywalder in forum Advanced Java
    Replies: 11
    Last Post: 09-01-2009, 02:48 PM
  5. simple problem - code wont compile
    By dirtycash in forum New To Java
    Replies: 1
    Last Post: 11-20-2007, 05:49 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •