已经实现的代码都在GitHub,欢迎拷打:yoo2i/cs61b (github.com)
proj0
单纯熟悉Java语法的,没啥坑,略过。
proj1
看完第11节才能学完迭代器和类方法的重写,写的时候没学过的不用写
要求里会让你抽象出一个deque的接口,没有这个接口自动评分机编译过不去的,还是那句话,别着急
编写随机测试对照组出现空指针错误
原因是deque模板类型设为int,int没法接受空值的比较,改成Integer就ok(proj1ec文档里更详细解释了)
ArrayDeque建议
最好用循环数组,维护好前后的两个指针,最好把移动这两个指针抽象出来做成函数,以后不管是扩容还是其他都调用函数就完事
课上教的instanceof语法评分机报错
1
| o instanceof LinkedListDeque test
|
这是高版本java特性,评分机没这么先进,强制类型转换就好了。
1 2 3
| if (o instanceof LinkedListDeque) { LinkedListDeque test = (LinkedListDeque) o; }
|
(已解决)关于这个proj我自己也存在一个问题,是在重写两个deque实现的equals方法的时候:
最开始我的写法是这样
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| @Override public boolean equals(Object o) { if (this == o) { return true; }
if (o instanceof LinkedListDeque) { LinkedListDeque test = (LinkedListDeque) o; if (this.size() == test.size()) { for (int pos = 0; pos < size; pos++) { T param1 = get(pos); Object param2 = test.get(pos); if (!param1.equals(param2)) { return false; } } return true; } return false; } if (o instanceof ArrayDeque) { ArrayDeque test = (ArrayDeque) o; if (this.size() == test.size()) { for (int pos = 0; pos < size; pos++) { T param1 = get(pos); Object param2 = test.get(pos); if (!param1.equals(param2)) { return false; } } return true; } return false; } return false; }
|
把两种情况拎出来讨论的,但是评分机对这种实现会报错:
1 2 3
| Test Failed! LinkedListDeque and ArrayDeques with the same elements should be equal at AGTestLinkedListDeque.test15:519 (AGTestLinkedListDeque.java)
|
后来我想起已经抽象出deque接口了,改了一下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| @Override public boolean equals(Object o) { if (this == o) { return true; }
if (o instanceof Deque) { Deque test = (Deque) o; if (this.size() == test.size()) { for (int pos = 0; pos < size; pos++) { T param1 = get(pos); Object param2 = test.get(pos); if (!param1.equals(param2)) { return false; } } return true; } return false; } return false; }
|
这样子就没bug了,但是,why????????
我觉得上面的实现也没啥bug啊,就是啰嗦一点;如果有朋友读到这发现上面那种实现的bug的话请和我联系:yoo2i@qq.com,感激不尽!
有朋友提出的原因是测评代码的对比样例可能有一部分并不是咱们实现的ArrayDeque和LinkedListDeque,而是标准库里面的这两个;标准库中的实现同样继承了Deque接口,这就导致了第一种实现无法识别导致报错,而第二种实现可以运行!
感谢向我提出这个想法的老哥,这是他的主页:https://yizhenallen.github.io
proj2
咕咕了太久了,这段时间处在人生的重要节点,写完一直没心情整理,今天发出来啦。
内容很多关于这个proj,所以给他单开了一章:gitlet-design | 柚子のBlog (yoo2i.github.io)