java - Returning a linked list containing keys from another Linked List -


i have following problem. have linked list or positional list containing objects called entry. each entry stores key , value pair. want make linked list grab list keys. have come method so,however reason order in added them not represented when print key list. have following 2 methods:

public positionallist1<k> keyset() //i prnted position position while adding , worked. tested addafter positionallist1 class , worked {     positioninterface<entry> iterator = map.first(); //gets first position in entry list     positioninterface<k> first = keylist.addfirst((k)iterator.getdata().getkey()); //adds list containing keys key entry list(getdata method returns whatever object stored @ node, in case entry object)     iterator = map.after(iterator); //go next node in entry list      for(int i=0;i<size-1;i++) //get rest of keys     {         positioninterface<k> p = keylist.addafter(first,(k)iterator.getdata().getkey());         iterator = map.after(iterator);      }     return keylist; }  public void printkeyset(positionallist1 list) //print key list {     positioninterface p = list.first();     for(int i=0; i<list.size();i++)     {         system.out.println("key : " + p.getdata());         p = list.after(p);     } } 

the keyset() method returns list containing keys, while printkeyset takes result of keyset() , prints entire key list. have tested using following main program:

orderedmapl<integer,string> map2 = new orderedmapl<integer,string>();     map2.put(2,"a");//adds (2,a)     map2.put(5,"b");//adds(5,b)     map2.put(1,"c");//adds(1,c)     map2.put(4,"d");//adds(4,d)     map2.put(3,"e");//adds(3,e)     map2.remove(2); //removes (2,a) 

this resulted in ordered list of (1,c) (2,a) .. etc , entries print fine in order. problem arises when calling following:

positionallist1<integer> keylist = map2.keyset(); map2.printkeyset(keylist); 

for reason keys printing in order: 1,5,4,3 instead of 1,3,4,5 , have no idea why. appreciated.

the problem line:

positioninterface<k> p = keylist.addafter(first,(k)iterator.getdata().getkey()); 

you shouldn't add after first. why wrong order.

if keys 1, 3, 4, 5 adding this:

  1. add 1 first element 1
  2. add 3 after first element 1 3
  3. add 4 after first element 1 4 3
  4. add 5 after first element 1 5 4 3

it because adding after first element.

if understand code correctly should change this:

// suspect #addfirst method returns added list element positioninterface<k> last = keylist.addfirst((k) iterator.getdata().getkey()); iterator = map.after(iterator);  for(int = 0; < size - 1; i++) {     // suspect #addafter method returns added list element     positioninterface<k> last = keylist.addafter(last, (k) iterator.getdata().getkey());     iterator = map.after(iterator);  } 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -