[語法] 泛型&巢狀類別語法

作者: scott0528 (Solar)   2017-03-19 18:12:45
各位前輩好,小弟在寫,Duck,Duck,Goose的遊戲(從資料結構的書上練習題而來)。
但是碰到泛型還有巢類別的語法問題如下。請直接跳到程式最下面(倒數第五行)的中文註
解。
import java.util.Random;
public class CircularlyLinkedList <E>
{
private static class Node<E>
{
private E element;
private Node<E> next;
public Node(E e, Node<E> n)
{
element = e;
next = n;
}
public E getElement( )
{ return element; }
public Node<E> getNext( )
{ return next; }
public void setNext(Node<E> n)
{ next = n; }
}
// instance variables of the CircularlyLinkedList
private Node<E> tail = null;
private int size = 0;
public CircularlyLinkedList( ) { }
// access methods
public int size( ) { return size; }
public boolean isEmpty( ) { return size == 0; }
public E first( ) {
if (isEmpty( )) return null;
return tail.getNext( ).getElement( ); ///???
}
public E last( )
{
if (isEmpty( )) return null;
return tail.getElement( );
}
// update methods
public void rotate( )
{
if (tail != null)
tail = tail.getNext( );
}
public void addFirst(E e)
{
if (size == 0)
{
tail = new Node<>(e, null);
tail.setNext(tail);
}
else
{
Node<E> newest = new Node<>(e, tail.getNext( ));
tail.setNext(newest);
}
size++;
}
public void addLast(E e)
{
addFirst(e);
tail = tail.getNext( );
}
public E removeFirst( )
{
if (isEmpty( )) return null;
Node<E> head = tail.getNext( );
if (head == tail) tail = null;
else tail.setNext(head.getNext( ));
size
作者: pttworld (批踢踢世界)   2017-03-19 18:48:00
把new好的Node丟進去,要先做new Node的工作語法上是C.new Node(....)這樣,指定存到某個變數當成傳進下一new Node的參數next,才串起來一開始最尾端當然傳null

Links booklink

Contact Us: admin [ a t ] ucptt.com