Page 3 of 3 FirstFirst 123
Results 41 to 50 of 50
  1. #41
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Hmmm...no, i dont think so.
    What do you mean by that?
    When I put them in a set should I sort them somehow?
    How should I do it in the following code?

    Java Code:
    private void printDogs(){
    	Set<Dog> doggies = new HashSet<Dog>(dogs);
    	Iterator<Dog> i = doggies.iterator();
    	while(i.hasNext()){
    		Dog d = (Dog) i.next();
    		System.out.println(d.getName());
    	}	
    }

  2. #42
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    You are talking about a dog at position 1 e.t.c That means the dogs obey some sort of ordering. Otherwise how do you know which one is number one and which one is number two? You can use a

  3. #43
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    You are talking about a dog at position 1 e.t.c That means the dogs obey some sort of ordering. Otherwise how do you know which one is number one and which one is number two? You can use a
    Hmmm... did i miss something or did your sentence end up suddenly :)
    Well I did this to show the number before the dog
    Java Code:
    private void printDogs(){
    	Set<Dog> doggies = new HashSet<Dog>(dogs);
    	Iterator<Dog> i = doggies.iterator();
    	int index = 0;
    	while(i.hasNext()){
    		index++;
    		Dog d = (Dog) i.next();
    		System.out.println(index + ") "+d.getName());
    	}
    }
    It prints correctly even though there is two dogs named Jeff and two Dougs
    and one Craig
    1) Jeff
    2) Doug
    3) Craig

    Now the Arraylist contains actually Objects Jeff,Jeff,Doug,Doug,Craig and according to that list if user selects number 3 = Craig and I use this code
    Java Code:
    Dog d = dogs.get(choice); //Gets the dog at index 3
    It will bring up Doug dog. How can I make it point out to right selection.

  4. #44
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    The forum Gremlins ate away the rest of my post above.
    You need to think carefully about these requirements. You need a TreeMap instead of a TreeSet.
    The map should map position (Integer) to Dog (or Dog name).
    You would just loop through the list adding the Dogs into the map only if a Dog with a similar name is not already in.

  5. #45
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Hmmm... I am getting prety confused here :)
    Do I then need all these equals and hashCode at my Dog class?
    Can you give me an code example where ArrayList is put into TreeMap and then put Object into to that map only if the object with similar name is not already in?

  6. #46
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Yes, you still need those methods. The reason why you need a different structure is because you can't use your ArrayList to get the values anymore. If the list had Jeff,Jeff,Doug,Doug,Craig and the user say they want number three the List's third item is not what they want. You can write that loop easily yourself. Just an if test here and there will get you home. See the API specs for the Map interface to see the methods you need to be using.

  7. #47
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    I am getting even close to the right solution? I am having difficulty to compare if it already has a same named dog. And then printing it out as mentioned in earlier posts

    Java Code:
    		Map<Integer, String> doggies = new TreeMap<Integer, String>();
    		int index = 0;
    		Iterator<Dog> i = dogs.iterator(); 
    		while(i.hasNext()){
    			Dog d = (Dog) i.next();
    			if(!doggies.containsValue(d.getName())){
    				index++;
    				doggies.put(index, d.getName());
    				}		
    				System.out.println(doggies.entrySet());
    			}
    		}

  8. #48
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Ok, I modified code to this

    Java Code:
    		Map<Integer, String> doggies = new TreeMap<Integer, String>();
    		int index = 0;
    		Iterator<Dog> i = dogs.iterator(); 
    		while(i.hasNext()){
    			Dog d = (Dog) i.next();
    			if(!doggies.containsValue(d.getName())){
    				index++;
    				doggies.put(index, d.getName());
    				}		
    			}
    		Set set = doggies.entrySet();
    		Iterator ite = set.iterator();
    		while(ite.hasNext()){
    			Map.Entry me = ((Map.Entry)ite.next());
    			System.out.println(me.getKey()+ " " + me.getValue());
    			
    		}
    	}
    Seems like it is printing it right. Does it seem ok to you, or is there a simple way of doing this whole thing?
    Well there is still one problem. Let say i make a int for the number of choice the user makes. How do i now retrieve the name of the dog by the number that user inputs?
    Last edited by soketti; 10-28-2009 at 09:05 PM.

  9. #49
    soketti is offline Member
    Join Date
    Oct 2009
    Posts
    26
    Rep Power
    0

    Default

    Anyone got ideas for above problem?

  10. #50
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Did you read the API specs for the Map interface to learn about the get method?

Page 3 of 3 FirstFirst 123

Similar Threads

  1. ArrayList with different objects? Help
    By xtrmi in forum New To Java
    Replies: 4
    Last Post: 02-27-2009, 08:51 PM
  2. Help printing specific ArrayList elements
    By CirKuT in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 12:24 AM
  3. Replies: 2
    Last Post: 11-08-2008, 10:40 AM
  4. Retrieving out objects from Vector
    By tuckker in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 05:52 AM

Posting Permissions

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