尝试输出链表时,我一直收到空异常错误。
当我只打印条件名称并且空的可以测试值!!!时,我没有收到该错误?不确定原因?
但是当我尝试遍历整个链表以将其打印出来时,我得到了Null异常错误
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();
}
}
答案 0 :(得分:1)
您的循环条件会检查列表中是否包含至少一个元素,但之后您尝试从列表中删除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();
}