Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Sorting using Iterator

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sorting using Iterator

    Hello
    This is my first post here since it is the first time i really needed help with my code.
    My problem is that I have to write code using an iterator that will sort entries from integers 1-9. The sorting must be done exactly when the program gets the entry. the entities are added automatically in the link list and this has to be done for all next entries. it has to be done in descending order of 1-9.
    this is what i have so far

    	public class PriorityFlightQueue2 {
     
    		private LinkedList<Flight> flights;
    		private Flight x;
     
     
    	public PriorityFlightQueue2()
    	{
    		flights=new LinkedList<Flight>();
    	}
     
    	public void joinQueue(String flightID,int priority)
    		{
    		x=new Flight(flightID,priority);
    		flights.add(x);
    		Iterator<Flight> it=flights.iterator();
    		while(it.hasNext())
    		{
     
    		}
     
     
    		}


    Thanks in advance,
    Palaboui


  2. #2
    Junior Member
    Join Date
    Nov 2010
    Posts
    7
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: Sorting using Iterator

    a better way to keep the flights sorted is to added in a sorted way in the first place instead of adding it and then sorted.
    rearrange the joinqueue function like this
    x=new Fligh();
    Iterator<Flight> it=flights.iterator();
    while(it.hasNext())
    {
    if(x.ID >=it.ID){flights.add(x)}

    }

Similar Threads

  1. Help With Iterator code... please help me..
    By basketball8533 in forum Collections and Generics
    Replies: 0
    Last Post: November 20th, 2011, 11:44 PM
  2. LinkedList Iterator
    By cpguy in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 16th, 2011, 09:51 PM
  3. stuck on Iterator
    By Mjall in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 30th, 2011, 12:00 AM
  4. iterator help needed
    By 999cm999 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 29th, 2011, 01:32 PM
  5. Iterator Problem
    By chrisych in forum Algorithms & Recursion
    Replies: 2
    Last Post: February 10th, 2010, 01:29 PM