java - 使用 Queue 打印出第 n 个字符串

标签 java queue

我想使用打印出第n个字符串 队列数据类型。

例如。

$ java NthString 5
a b c d e f
< ctrl -d >

应该给我:

b (the fifth string from the right)

这是我到目前为止所拥有的,但我不知道下一步:

public class NthString {

   public static void main(String[] args) {

      Queue<Integer> q = new Queue<Integer>();

      while(!StdIn.isEmpty()){
         q.enqueue(StdIn.readInt());
      }
   }
}

谢谢

最佳答案

public class NthString {

   public static void main(String[] args) {
      Integer n = Integer.parseInt(args[0]);
      Queue<Integer> q = new Queue<Integer>();
      while(!StdIn.isEmpty()){
         q.enqueue(StdIn.readInt());
      }
      while(q.size() > n){
         q.dequeue();
      }
      StdOut.println(q.peek().toString());
   }

}

关于java - 使用 Queue 打印出第 n 个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35927307/

相关文章:

Python多处理队列内存管理

laravel - 如何从 Laravel 中的作业 ID 获取排队的作业?

java - 将 Pojo 列表转换为嵌套列表

java - 将java应用程序部署到azure web应用程序容器时出现"Connection timed out: connect"

java - 当设备处于 sleep 状态时在后台运行广播接收器?

java - MySQL查询抛出异常,而不是一个空的ResultSet

queue - 分布式限速

java - 队列没有产生正确的输出

c# - 在 C# 中是否可以使用 {a,b,c} 实例化队列?

javascript - 从 JSP 获取隐藏值的不同方法