×

# 算法练习(34): Queue练习(1.3.15-1.3.17)

### 知识点

• Queue的enqueue、dequeue

### 题目

1.3.15 编写一个Queue的用例，接受一个命令行参数k并打印出标准输入中的倒数第k个字符串（假设标准输入中至少有k个字符串）。

1.3.15 Write a Queue client that takes a command-line argument k and prints the kth from the last string found on standard input (assuming that standard input has k or more strings).

### 答案

``````public class QueueExecise1 {

public static void main(String[] args) {
// TODO Auto-generated method stub
String a[] =
{
"我的",
"名字",
"叫",
"顶级程序员不穿女装",
"微博:https://m.weibo.cn/p/1005056186766482"
};

int k = 3;
Queue<String> stringQueue = new Queue<String>();
for (int i = 0; i < a.length; ++i) {
stringQueue.enqueue(a[i]);
}

int index = a.length - k;
for (int j = 0; j <= index; ++j) {
String dequeuedString = stringQueue.dequeue();
if (j == index)
System.out.print(dequeuedString);
}
}
}
``````

### 代码索引

QueueExecise1.java

### 题目

1.3.16 Using readInts() on page 126 as a model,write a static method readDates() for Date that reads dates from standard input in the format specified in the table on page 119 and returns an array containing them.

### 分析

API for our library of static methods for standard input

### 答案

``````public class Date1 {

private int month;
private int day;
private int year;

public Date1(String date) {
String[] fields = date.split("/");
month = Integer.parseInt(fields[0]);
day = Integer.parseInt(fields[1]);
year = Integer.parseInt(fields[2]);
}

public String toString() {
return "" + month + "/" + day + "/" + year;
}

public static Date1[] readDates(String name) {
In in = new In(name);
Queue<Date1> q = new Queue<Date1>();
while (!in.isEmpty()) {
q.enqueue(date1);
}
int N = q.size();
Date1[] a = new Date1[N];
for (int i = 0; i < N; i++)
a[i] = q.dequeue();
return a;
}

public static void main(String[] args) {
String filePathString = System.getProperty("user.dir");

String dateFileString = filePathString
+ "/src/com/kyson/chapter1/section3/" + "b.txt";
System.out.println("即将读取" + dateFileString + "文件中得到的数组为：");
System.out.println("读取文件中得到的数组为：");
for (int i = 0; i < date1.length; i++) {
System.out.println(date1[i]);
}
}
}
``````

Date1.java

### 题目

1.3.17 为Transaction 类完成练习1.3.16

1.3.17 DoExercise 1.3.16 for Transaction.

### 答案

``````public class Transaction1 {

private Date1 when;
private String who;
private double amount;

public Transaction1(String date) {
String[] fields = date.split("-");
when = new Date1(fields[0]);
who = fields[1];
amount = Integer.parseInt(fields[2]);
}

public String toString() {
return "" + this.who + " at " + this.when + " transaction "
+ this.amount;
}

public static Transaction1[] readTransactions(String name) {
In in = new In(name);
Queue<Transaction1> q = new Queue<Transaction1>();
while (!in.isEmpty()) {
q.enqueue(transaction1);
}
int N = q.size();
Transaction1[] a = new Transaction1[N];
for (int i = 0; i < N; i++)
a[i] = q.dequeue();
return a;
}

public static void main(String[] args) {
// TODO Auto-generated method stub

String filePathString = System.getProperty("user.dir");

String intFileString = filePathString
+ "/src/com/kyson/chapter1/section3/" + "c.txt";
System.out.println("即将读取" + intFileString + "文件中得到的数组为：");
System.out.println("读取文件中得到的数组为：");
for (int i = 0; i < transaction1s.length; i++) {
System.out.println(transaction1s[i]);
}
}

}
``````

### 代码分析

``````//获得程序当前路径
System.getProperty(“user.dir”)
``````

### 思考

16、17的意义在于，它让我们知道如何读取并正确输出信息。就是【先enqueue后dequeue】

### 代码索引

Transaction1.java