知识点
- 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[] =
{
"我的",
"名字",
"叫",
"顶级程序员不穿女装",
};
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);
}
}
}
代码索引
题目
1.3.16 使用1.3.1.5节中的readInts()作为模板为Date编写一个静态方法readDates(),从标准输入中读取由练习1.2.19的表格所指定的格式的多个日期并返回一个它们的数组。
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.
分析
看英文部分写的是,126 页,只是中文翻译的时候改成了1.3.1.5
下面是中文版的部分
先进先出队列1
先进先出队列2
对照着英文版:
英文版
为了方便没有从前往后看的读者理解readInts这个方法,我这里拉取了In的API,以及实现代码如下
API for our library of static methods for standard input 构造函数
readString
参照习题1.2.19完成readDates()即可。
答案
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()) {
String readedString = in.readString();
Date1 date1 = new Date1(readedString);
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 + "文件中得到的数组为:");
Date1[] date1 = readDates(dateFileString);
System.out.println("读取文件中得到的数组为:");
for (int i = 0; i < date1.length; i++) {
System.out.println(date1[i]);
}
}
}
代码索引
题目
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()) {
String readedString = in.readString();
Transaction1 transaction1 = new Transaction1(readedString);
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 + "文件中得到的数组为:");
Transaction1[] transaction1s = readTransactions(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】