下面是我的代碼爲我的DList接受對象。它打印出除了最後一個打印語句之外的所有內容。 「的System.out.println((int)的(list.head.next.next.next.item.getItem()));」這是爲什麼?以及如何解決它?從列表中訪問對象的方法。
/* DList1.java */
/**
* A DList1 is a mutable doubly-linked list. (No sentinel, not
* circularly linked.)
*/
public class DList1 {
/**
* head references the first node.
* tail references the last node.
*
* DO NOT CHANGE THE FOLLOWING FIELD DECLARATIONS.
*/
protected DListNode1 head;
protected DListNode1 tail;
protected long size;
public DList1() {
head = null;
tail = null;
size = 0;
}
public DList1(Object a) {
head = new DListNode1();
tail = head;
head.item = a;
size = 1;
}
public DList1(Object a, Object b) {
head = new DListNode1();
head.item = a;
tail = new DListNode1();
tail.item = b;
head.next = tail;
tail.prev = head;
size = 2;
}
public void insertFront(Object i) {
DListNode1 temp = new DListNode1(i);
if (size == 0) {
head = temp;
tail = temp;
}
else {
temp.next = head;
head.prev = temp;
head = temp;
} size++;
}
public void insertEnd(Object i) {
DListNode1 temp = new DListNode1(i);
if (size == 0) {
head = temp;
tail = temp;
}
else {
tail.next = temp;
temp.prev = tail;
tail = temp;
} size++;
}
public void removeFront() {
if (size == 0) {
return;
}
else if (size == 1) {
head = null;
tail = null;
size--;
}
else {
head = head.next;
head.prev = null;
size--;
}
}
public String toString() {
String result = "[ ";
DListNode1 current = head;
while (current != null) {
result = result + current.item + " ";
current = current.next;
}
return result + "]";
}
public static void main(String[] args) {
int[] array = new int[2];
array[0] = 3;
array[1] = 4;
int m = 1;
String g = "hi";
String s = "boo";
String z = "foo";
DList1 list = new DList1(m);
tobject jim = new tobject();
//list.insertFront(g);
list.insertEnd(s);
list.insertEnd(g);
list.insertEnd(jim);
System.out.println((list.head.item));
System.out.println((list.head.next.item));
System.out.println((list.head.next.next.item));
System.out.println((int)(list.head.next.next.next.item.getItem()));
System.out.println(list.size);
//System.out.println((int)(list.head.next.item[0]));// expected 3 but failed
//System.out.println(((String) list.head.next.item));
}
}
//////////////
/* DListNode1.java */
/**
* A DListNode1 is a node in a DList1 (doubly-linked list).
*/
public class DListNode1{
/**
* item references the item stored in the current node.
* prev references the previous node in the DList.
* next references the next node in the DList.
*
* DO NOT CHANGE THE FOLLOWING FIELD DECLARATIONS.
*/
public Object item;
public DListNode1 prev;
public DListNode1 next;
/**
* DListNode1() constructor.
*/
DListNode1() {
//item = NULL;
prev = null;
next = null;
}
DListNode1(Object i) {
item = i;
prev = null;
next = null;
}
}
////////////
public class tobject {
private int pai;
public tobject(){
pai = 3;
}
public int getItem(){
return pai;
}
}
項目是一個「對象」。它沒有任何'getItem()'方法。 – NeplatnyUdaj
你在'insertend'中的其他部分看起來有點粗略?它將'temp'添加到'tail.next',然後將'temp.prev'設置爲'tail',然後使'tail'成爲'temp'? – Asthor