[语法] 泛型&巢状类别语法

楼主: 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