我想使用打印出第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/