当我试图输出我的链表时,我一直得到null异常错误。
当我只打印条件名称和空的can测试值时,我不会得到错误!!!?不确定原因是什么?
但是,当我试图在整个链表中进行迭代以将其打印出来时,我会得到Null Exception错误
public class ItemLinkedList {
private ItemInfoNode head;
private ItemInfoNode tail;
private int size = 0;
public int getSize() {
return size;
}
public void addBack(ItemInfo info) {
size++;
if (head == null) {
head = new ItemInfoNode(info, null, null);
tail = head;
} else {
ItemInfoNode node = new ItemInfoNode(info, null, tail);
this.tail.next = node;
this.tail = node;
}
}
public ItemInfo removeFront() {
ItemInfo result = null;
if (head != null) {
size--;
result = head.info;
if (head.next != null) {
head.next.prev = null;
head = head.next;
} else {
head = null;
tail = null;
}
}
return result;
}
public static void main(String[] args) {
ItemLinkedList list = new ItemLinkedList();
list.addBack( new ItemInfo("Bicipital Tendonitis", 1, 0, 1, 1) );
list.addBack( new ItemInfo("Coracoid Impingement", 0, 1, 1, 1) );
list.addBack( new ItemInfo("Supraspinatus Impingement", 1, 0, 0, 1) );
list.addBack( new ItemInfo("Bicipital Tendonitis", 1, 0, 1, 1) );
list.addBack( new ItemInfo("Glenohumeral Dislcation", 0, 0, 1, 1) );
list.addBack( new ItemInfo("Clavicular Fracture", 1, 0, 1, 0) );
list.addBack( new ItemInfo("Labral Tear", 1, 1, 0, 0) );
list.addBack( new ItemInfo("SubAcromial Bursitis", 1, 0, 0, 0) );
while (list.getSize() > 0){
System.out.println( "Condition Name " + list.removeFront().getCondName() );
System.out.println( "t Empy Can Test: " + list.removeFront().getEmptyCanTest() );
System.out.println( "t Speed's Test: " + list.removeFront().getSpeedsTest() );
System.out.println( "t Apprehension Test: " + list.removeFront().getApprehensionTest() );
System.out.println( "t Pain Provocation Test: " + list.removeFront().getpainProvocationTest() );
System.out.println();
}
}
循环的条件检查列表中是否至少有一个元素,然后尝试从列表中删除5个元素。
您应该在每次迭代中只调用removeFront
一次:
while (list.getSize() > 0){
ItemInfo item = list.removeFront();
System.out.println( "Condition Name " + item.getCondName() );
System.out.println( "t Empy Can Test: " + item.getEmptyCanTest() );
System.out.println( "t Speed's Test: " + item.getSpeedsTest() );
System.out.println( "t Apprehension Test: " + item.getApprehensionTest() );
System.out.println( "t Pain Provocation Test: " + item.getpainProvocationTest() );
System.out.println();
}