ArrayList和队列

时间:2013-11-23 01:37:16

标签: java arrays arraylist queue

我的程序,简而言之,应该采用一个mainQueue LinkedList的整数,一个一个地查看它们,然后对它们进行排序。它检查每个整数的最后一位数,并将它们放入相应的subQueues中。截至目前,我只到那个地方。并插入其子队列。但是,我无法弄清楚如何获取所有数字并对它们进行排序并显示它们。这是一个例子。

 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





}

}

1 个答案:

答案 0 :(得分:0)

您尝试使用LinkedList做的不是最佳选择。也就是说,您可以随时调用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]);
}