[关闭]
@w1024020103 2017-02-16 13:31 字数 1749 阅读 1973

CS61B Proj1A notes

CS61B UCBerkeley java


When writing LinkedListDeque.java, I ran into some prolems. The given API says things like:
public void addFirst(Item): Adds an item to the front of the Deque.
public void addLast(Item): Adds an item to the back of the Deque.

I didn't get what does Item mean here, should I just write exactly Item or should I replace it withString or int?
I got compiler errors when I wrote Item, so I replaced it with String since the LinkedListDequeTest.java first created a new LinkedListDeque of strings(line 40).

However, I got compiler error again as follows:

image_1b8mfi1igdaj1o7a1td41q65r589.png-34.1kB

Then I asked people in wechat study group for help, and they helped me out with this problem. I added<Item> after the class name to meet the requested generic type as follows:

  1. public class LinkedListDeque<Item>{
  2. private class Node<Item>{
  3. Item item;
  4. Node next, prev;

But this is not enough, I had to add<Item> to other places as well:

  1. private int size;
  2. private Node <Item> sentinel, last;
  1. public <Item> Item removeFirst(){
  2. if(sentinel.next == null){
  3. return null;
  4. }
  5. Item item = <Item> sentinel.next.item;
  6. sentinel.next = sentinel.next.next;
  7. size -= 1;
  8. return item;
  9. }
  1. public <Item> Item removeLast(){
  2. Item item = (Item)last.item;
  3. last = last.prev;
  4. last.next = sentinel;
  5. size -= 1;
  6. return item;
  7. }

Seems like I added <Item> to every related class and method, especially I used (Item) for casting. This caused a whitle and finally got some help from the wechat study group.

Later, I was stuck by a problem:
image_1b8qoej418j3mb1flf43415rd9.png-31.1kB

I searched the webs for help, most of the talks were about mistakes in the number of }, some were about GBK and UTF-8. Ironcally, I spent almost half a day finding where went wrong but in vain. Looking back, i guess i was just pissed off and didn't have patience. So, this is a good practice when you are faced with a unknown problem, just stay patient and carefully analyze it.

It turned out I added an extra } when testing parts of the program, and I forgot to delete it when wrote some new parts.
image_1b8qonpjs1cer19gv3201s9j1ntlm.png-9.4kB

Great, finally things went on well.
image_1b8qoqulgdbbngr1o2cgka1lre1t.png-18.1kB

ArrayDeque

I ran into problems when writing resize() method for ArrayDeque.java.

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注