2013-11-23 5 views
0

내 프로그램은, 짧은에, 정수의 mainQueue LinkedList의을 하나씩 모두보고, 그들을 정렬 할 예정이다. 각 정수의 마지막 자리를 조사해, 대응하는 subQueues에 배치합니다. 현재로서는, 나는 단지 장소입니다. 그 서브 큐에 삽입됩니다. 그러나 모든 숫자를 가져 와서 정렬하고 표시하는 방법을 알아낼 수 없습니다. 다음은 그 예입니다. 당신이 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

최적이 아니다. 그것은 당신이 항상 종류 및 인쇄 한 후, 다음과 같이 배열을 얻을 수 toArray()를 호출 할 수 있습니다 말했다 :

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]); 
}