Results 1 to 7 of 7

Thread: hashmaps

  1. #1
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Unhappy hashmaps

    Hello im making a program that processes lists .. but im having problems with hashmaps. Im gonna show u my problem in this method .. please read the comments to understand it =)

    Firstly :
    Ways = Arraylist
    waynodes = int array
    hashnode = hashmap containing node objects .. these objects contains the ArrayList grannNodes.

    Java Code:
     
    
    private void getNeighbours(){
    // going trough all of the ways..
           for (int way = 0 ;way < Ways.size();way++){
             // goign though all of the nodes
               for (int nod =0; nod < Ways.get(way).waynodes.length;nod++){
                  // if a node is the first one in the way... 
                   if ( nod == 0 ) {
     // at the hashnode where the name is the same as in the list i want to ad the next node only                 
    hashnode.get(Ways.get(way).waynodes[nod]).grannNodes.add(Ways.get(way).waynodes[nod + 1]);
    
    
                   }
                   
                   
               }
    
           }
       
       }
    My problem is when i check the objects inside of hashnode it has added to all objects the same nodes ... so when in the objects it should be size 1 (becasue 0 only has neighbour 1) its size is 169... Sorry for my english i know its crap =P.. anyway .. Hope you guys can help me figure out whats wrong =) thanks

  2. #2
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    the problem is, you looped through the whole list and added all of them

    when you find the node you want, you should break the loop and add only that one.


    Java Code:
    Object o = null;
    OuterLoop:
    for ( : ; ) {
        for ( : ; ) {
            boolean foundIt = something==true;
            if (foundIt) {
                //get object;
                o = theObject;
                //stop looping
                break OuterLoop;
            }
        }
    }

  3. #3
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default

    I dont think this is my problem ... but thanks for reply ..

    What i want to do is :

    Java Code:
    //loop trough all of the list
    for list a:::
    // loop trough all of the objects
    for all objects in list a :::
                take the object before this object and the object after this object and put them in a ArrayList in this object ...
    so im in the first stage 0 where i just have one neighbor ... 1 .. I try to take him and only add him to the current 0 object but every time i get a 0 object it adds it to ALL objects in all lists =S

  4. #4
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    don't try to do it all in one line though.
    Java Code:
    //make a list
    List<object> currentList = new ArrayList<object>();
    //add required objects to list
    currentList.add(get the previous object);
    currentList.add(get the next object);
    //copy the list into the current object
    currentObject.setList(currentList);

  5. #5
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default

    thanks man ... I'll try that ...=P have some homework to do first =) thanks

  6. #6
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default

    well ... its like ... I'v already added everything to the lists ... i just whant to get the neighbors =P

  7. #7
    santa's Avatar
    santa is offline Senior Member
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    208
    Rep Power
    6

    Default

    Solved . For some reason i put one of my lists static ... and it added everything to it because it was the only list =P sorry and thanks

Similar Threads

  1. how can I compare HashMaps with different keys ?
    By aneuryzma in forum New To Java
    Replies: 6
    Last Post: 04-05-2011, 04:12 PM
  2. Concerning HashMaps
    By hjensen in forum New To Java
    Replies: 7
    Last Post: 10-18-2010, 04:36 PM
  3. Problems with HashMaps
    By li_bi in forum New To Java
    Replies: 13
    Last Post: 02-24-2010, 01:08 PM
  4. HashMaps get key for a specific value
    By andre1011 in forum Advanced Java
    Replies: 2
    Last Post: 03-11-2009, 03:30 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
  •