java - Having trouble removing from a queue and adding to another -


i have input file containing many strings , ints.

ex.

blah 40

hello 10

asdf 20

etc...

i have read them queue hold them. need take them out of queue , add priority queue whenever int equals ints in data file.

this have far..

for(int = 0; i<=50; i++) {     object x = normalqueue.dequeue();  //this makes x equal line of data file dequeued.     if(i == x.secondint) //secondint objects method gets integer in data file     {         pqueue.insert(x); //inserts x pqueue if = second int in data file     }     else     {         normalqueue.enqueue(x); //adds x queue1         normalqueue.switchends(); //swaps 1st , last node     } } 

the problem having is printing out 2 files of data file.

i'm not quite sure intend happen when running code, happen this:

given following input:

blah 40 hello 10 asdf 20 

(i'm assuming add elements of queue when reading file, ordering data-file retained)

in first 40 iterations of loop taking "blah 40" out of queue1, adding "blah 40" queue1 , swapping "blah 40" either "asdf 20" or "hello 10" (depending on how many iterations you've gone through) "blah 40" again @ beginning of queue1.

in 41th iteration of loop, i going 40 , insert "blah 40" queue2.

in iteration 42 until last iteration of loop nothing done, except shuffling queue1 around, because i isn't 10 or 20 before "blah 40" removed, @ end of loop queue1 contains "hello 10" , "asdf 20", whilst queue2 contains "blah 40".

i realize walk-through might little confusing, try print out contents of both queues after each iteration , should become clearer.

edit: sounds me you're trying achieve priority ordering on second ints (i.e. 40, 10 , 20) when loop through queue2 , remove 1 , 1 element print out so:

"hello 10" "asdf 20" "blah 40" 

(or reverse). if case, there's no need queue1 @ all, need make sure supply priority queue custom comparator sorts objects on second ints. (alternatively implement comparable interface in custom objects).

example comparator:

new comparator<yourcustomobject>()  {       @override       public int compare(yourcustomobject  o1, yourcustomobject  o2)        {         return integer.compare(o1.secondint, o2.secondint);        } } 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -