2013-11-23 94 views
0

我的程序,總之,應該採取一個mainQueue LinkedList的整數,逐個查看它們,並對它們進行排序。它檢查每個整數的最後一位,並將它們放入相應的子隊列中。到現在爲止,我只是在那個地方。並插入到它的子隊列中。但是,我不知道如何獲取所有數字並對它們進行排序並顯示它們。這是一個例子。ArrayList和隊列

mainQueue = { 12 50 215 100 85 539 16 35 } // Original Queue The numbers in the queues are placedin the subqueues depending on last digit on number if number is 50 its placed into subqueue 0. All of this works but I can get the numbers to then be sorted and display. Help please. Sorry for the formation of the code 




subQueue[0] = { 50 100 } 
subQueue[1] = { } 
subQueue[2] = { 12 } 
subQueue[3] = { } 
subQueue[4] = { } 
subQueue[5] = { 215 85 35 } 
subQueue[6] = { 16 } 
subQueue[7] = { } 
subQueue[8] = { } 
subQueue[9] = { 539 } 
mainQueue = { 12 16 35 50 85 100 215 539 } 

import java.util.LinkedList; //LinkedList will be used as a queue 

public class Sorting 
    { 
private LinkedList<Object> mainQueue; 
private LinkedList[] subQueues; 
private final int SIZE = 10; 
private int maxDigits; //maximum number of digitszz 

//The constructor instantiates the mainQueue using the LinkedList, 
//subQueue array as an array of LinkedList using SIZE(10), 
//and initializes maxDigits = 0; 
public Sorting() 
{ 

    mainQueue = new LinkedList<Object>(); 
    subQueues = new LinkedList[SIZE]; 
    for (int i = 0; i < SIZE; ++i) { 
     subQueues[i] = new LinkedList(); 
    } 
    maxDigits = 0; 


} 


public void addToMainQueue(Integer num) 
{ 



    mainQueue.add(num); 

} 


//The listMaintQueue method returns a string containing 
//the content of the main-queue 
public String listMainQueue() 
{ 
    return ("mainQueue = " + listQueue(mainQueue)+"\n"); 
} 


//The listSubQueues method returns a string containing 
//the content of the sub-queues 
public String listSubQueues() 
{ 
    String result = ""; 

    for (int i=0; i<SIZE; i++) 
    { 
     result += "subQueue[" + i + "]:"; 
     result += listQueue(subQueues[i]); 
     result += "\n"; 
    } 
    return result; 
} 


//The listQueue method returns a string containing 
//the content of the parameter queue 
public String listQueue(LinkedList<Object> queue) 
{ 
    LinkedList<Object> temp = new LinkedList<Object>(); 
    String result = "{ "; 

    //Removing each element from the queue 
    //and appending to the string to be returned 
    while (!queue.isEmpty()) 
    { 
     Object removed = queue.remove(); 
     result += removed + " "; 
     temp.offer(removed); 
    } 
    result += "}\n"; 

    //Copying the temporary queue back to the 
    //original queue 
    while (!temp.isEmpty()) 
    { 
     Object removed2 = temp.remove(); 
     queue.offer(removed2); 
    } 
    return result; 
} 


//The sortNumbers method sorts numbers in the main queue. 
public void sortNumbers() //This class performs the sortin 
{ 


    while (mainQueue.isEmpty() == false) //loop that checks if array is empty and places the lst digit into its corresponding subqueue. 
    { 
     Object lead = mainQueue.peek(); 
     mainQueue.remove(); 
     String digits = "" + lead; 
     int digit = Integer.parseInt(digits.substring(digits.length()-1,  digits.length())); 



     subQueues[digit].offer(lead); 

    } 







    System.out.print(listSubQueues()); //Step 5 




    System.out.print(listMainQueue()); //Step 9 





} 

}

回答

0

你想用鏈表做的是不是最優的。這就是說,你可以隨時撥打指定者()來獲取一個數組作爲在下面,然後排序和打印:

String[] main_queue = (String[])listMainQueue.toArray(); 
java.util.Arrays.sort(main_queue); 

按照相同的步驟對所有隊列。然後,在一個循環中:

for(int i = 0; i < main_queue.length; i++) 
{ 
    System.out.println(main_queue[i]); 
}