如何使用队列对整数流进行排序?JAVA实验室

如何使用队列对整数流进行排序?JAVA实验室,java,sorting,integer,queue,modulo,Java,Sorting,Integer,Queue,Modulo,好的,这个程序需要使用一个队列来分离一个整数流。我得到了一个输出,但它的顺序不是期望的,首先是整数可以被3整除,然后是1%3,然后是2%3 有什么建议吗? 这是我的密码: import java.util.Random; public class Oct22 { public static void main(String[] arg) { int x; Random rnd = new Random(); Queue<

好的,这个程序需要使用一个队列来分离一个整数流。我得到了一个输出,但它的顺序不是期望的,首先是整数可以被3整除,然后是1%3,然后是2%3 有什么建议吗? 这是我的密码:

  import java.util.Random;

  public class Oct22
{

    public static void main(String[] arg)
   {
          int x;
       Random rnd =  new Random();
     Queue<Integer> queue = new ArrayQueue<Integer>();
Queue<Integer> queue2 = new ArrayQueue<Integer>();
     //for(char c='a'; c<= 'z'; c++)
    //   queue.add(c);

for(int i=0;i<20;i++)
  queue2.add(rnd.nextInt(100));


for(int i=0;i<queue2.size();i++)
{
 x=queue2.remove();
if(x%3==0)
  queue.add(x);
else
  queue2.add(x); 
}

for(int i=0;i<queue2.size();i++)
{
 x=queue2.remove();
if(x%3==1)
  queue.add(x);
else
  queue2.add(x);
}
 for(int i=0;i<queue2.size();i++)
{
 x=queue2.remove();
if(x%3==2)
  queue.add(x);
else 
  queue.add(x);     
}


System.out.println(queue.size());
System.out.println(queue2.size());

System.out.println("the size of queue is: " + queue.size());  
while(!queue.isEmpty())
   System.out.print(queue.remove()+ " ");
System.out.println("\n---------------------------------");
while(!queue2.isEmpty())
  System.out.print(queue2.remove()+" ");

}
import java.util.Random;
公共课十月二十二日
{
公共静态void main(字符串[]arg)
{
int x;
随机rnd=新随机();
Queue Queue=new ArrayQueue();
Queue queue2=新的ArrayQueue();

//对于每个循环中的(char c='a';c,更改以下内容:

for(int i=0;i<queue2.size();i++)
因为此时queue2中所有剩余的数字都应该有一个2的余数

完整代码:

      int x;
      int size;
      Random rnd =  new Random();
      Queue<Integer> queue = new ArrayDeque<Integer>();
      Queue<Integer> queue2 = new ArrayDeque<Integer>();

      for(int i=0;i<20;i++)
        queue2.add(rnd.nextInt(100));

      size = queue2.size();
      for(int i=0;i<size;i++)
      {
        x=queue2.remove();
        if(x%3==0)
          queue.add(x);
        else
          queue2.add(x); 
      }

      size = queue2.size();
      for(int i=0;i<size;i++)
      {
        x=queue2.remove();
        if(x%3==1)
          queue.add(x);
        else
          queue2.add(x);
      }

      size = queue2.size();
      for(int i=0;i<size;i++)
      {
        x=queue2.remove();
        if(x%3==2)
          queue.add(x);
        else 
          queue.add(x);     
      }

      System.out.println(queue.size());
      System.out.println(queue2.size());

      System.out.println("the size of queue is: " + queue.size());  
      while(!queue.isEmpty())
        System.out.print(queue.remove()+ " ");
      System.out.println("\n---------------------------------");
      while(!queue2.isEmpty())
        System.out.print(queue2.remove()+" ");
intx;
整数大小;
随机rnd=新随机();
Queue Queue=new ArrayDeque();
Queue queue2=new ArrayDeque();

对于(int i=0;i请显示一个样本输入、输出和所需输出。输入将是20个随机整数的第一个队列。我所需的输出将一个整数流分成一个流,其中包括:首先,可被3整除的整数,其次,等于1模3的整数,最后,等于2的整数模3。您的程序应该通过生成0到100之间的20个随机整数来模拟传入流,并且最多只能使用2个整数队列来解决问题output>33 45 39 36 21 43 4 37 77 38 31 14 90 71------------------------是输入还是输出?它应该是有序的,但也不是并不是所有的20个整数都是正确的!谢谢你,先生,还有一个问题。当试图使用toString()转换输出时,我如何保持它们的顺序相同?我在转换时会把它们弄得乱七八糟,因为我试图在joptiondialog框
public String toString(){String output=“”;中显示(int i=0;i@Cpt.Awesome我看不到您在哪里创建这个
缓冲区
数组,所以我不能说问题出在哪里。
queue.addAll(queue2);
      int x;
      int size;
      Random rnd =  new Random();
      Queue<Integer> queue = new ArrayDeque<Integer>();
      Queue<Integer> queue2 = new ArrayDeque<Integer>();

      for(int i=0;i<20;i++)
        queue2.add(rnd.nextInt(100));

      size = queue2.size();
      for(int i=0;i<size;i++)
      {
        x=queue2.remove();
        if(x%3==0)
          queue.add(x);
        else
          queue2.add(x); 
      }

      size = queue2.size();
      for(int i=0;i<size;i++)
      {
        x=queue2.remove();
        if(x%3==1)
          queue.add(x);
        else
          queue2.add(x);
      }

      size = queue2.size();
      for(int i=0;i<size;i++)
      {
        x=queue2.remove();
        if(x%3==2)
          queue.add(x);
        else 
          queue.add(x);     
      }

      System.out.println(queue.size());
      System.out.println(queue2.size());

      System.out.println("the size of queue is: " + queue.size());  
      while(!queue.isEmpty())
        System.out.print(queue.remove()+ " ");
      System.out.println("\n---------------------------------");
      while(!queue2.isEmpty())
        System.out.print(queue2.remove()+" ");