[英]Trouble understanding method for inserting nodes java
我想我已經走得很遠了,但是我處於邏輯掛斷狀態,也許你們中的一些聰明人可以幫助我!
public class ItemList{
ItemInfoNode head;
ItemInfoNode tail;
int listCount = 0;
public ItemList(){
head = tail = null;
}
public void insertInfo(String name, String rfidTag, String initPosition, double price){
ItemInfo obj = new ItemInfo(name, rfidTag, initPosition, initPosition, price);
ItemInfoNode temp = new ItemInfoNode();
temp.setInfo(obj);
if(head == null){ head = tail = temp; }
else{
if(head == tail){//BEGIND SECOND OBJECT HANDLING
if(head.getInfo().getTag().compareToIgnoreCase(rfidTag) <= 0){//to see if temp belongs after head
head.setNext(temp);
temp.setPrev(head);
tail = temp;
}
else{
ItemInfoNode nodePtr = head;
head = temp;
tail = nodePtr;
head.setNext(tail);
tail.setPrev(head);
}
}//END SECOND OBJECT HANDLING
else{
if(head.getInfo().getTag().compareToIgnoreCase(rfidTag) > 0){
ItemInfoNode nodePtr = head;
head = temp;
temp.setNext(nodePtr);
temp.getNext().setPrev(head);
}
else if(head.getInfo().getTag().compareToIgnoreCase(rfidTag) <= 0 && tail.getInfo().getTag().compareToIgnoreCase(rfidTag) > 0){
head.setNext(temp);
temp.setPrev(head);
}
else{//item bigger then tail
ItemInfoNode nodePtr = tail;
tail = temp;
tail.setPrev(nodePtr);
tail.getPrev().setNext(tail);
}
}
}
listCount++;
}}
現在,此方法的目的顯然是在它們所屬的節點上插入節點,但需要按其rfidTag字符串對它們進行排序,該字符串是一個十六進制數,我不確定是否明顯,但我想至少最大的順序。 現在,您可以看到我的代碼變得非常復雜,很難遵循和處理,但是我想我很親近,任何人都可以提供任何提示或“邏輯指導”,以幫助我更好地理解如何實現此目標正常工作? 在當前狀態下,它正在破壞我的列表,有些循環,然后拋出NullPointerException!
編輯**:所以我修改了代碼並添加了注釋,以更簡潔地解釋我要完成的工作,也許有人可以幫助我了解如何立即使用這些方法?
我現在已經很接近了,當我按對象在列表中的順序放置對象時,它可以工作,但是如果我嘗試插入一個屬於中間位置的對象節點,則會銷毀我的列表,那么我看不到錯誤,有人看到嗎? 主要供參考
public class Test{
public static void main(String args[]){
ItemInfo item = new ItemInfo(null, null, null, null, 0);
item.setName("Chocolate");
item.setTag("2");
item.setOrigin("s12345");
item.setCurrent("s12345");
item.setPrice(30.00);
ItemInfo item2 = new ItemInfo(null, null, null, null, 0);
item2.setName("Buzz Lightyear");
item2.setTag("1");
item2.setOrigin("d67890");
item2.setCurrent("d67890");
item2.setPrice(15.99);
ItemInfo item3 = new ItemInfo(null, null, null, null, 0);
item3.setName("Hotwheels");
item3.setTag("000000000");
item3.setOrigin("h34743");
item3.setCurrent("h34743");
item3.setPrice(24.25);
ItemInfo item4 = new ItemInfo(null, null, null, null, 0);
item4.setName("Barbie");
item4.setTag("FFFFFFFFF");
item4.setOrigin("s49862");
item4.setCurrent("s49862");
item4.setPrice(21.22);
ItemInfo item5 = new ItemInfo(null, null, null, null, 0);
item5.setName("Bicycle");
item5.setTag("CCCCCCCCC");
item5.setOrigin("k28475");
item5.setCurrent("k28475");
item5.setPrice(10.99);
ItemInfoNode nood = new ItemInfoNode();
ItemInfoNode nood2 = new ItemInfoNode();
ItemInfoNode nood3 = new ItemInfoNode();
ItemInfoNode nood4 = new ItemInfoNode();
ItemInfoNode nood5 = new ItemInfoNode();
nood.setInfo(item);
nood2.setInfo(item2);
nood3.setInfo(item3);
nood4.setInfo(item4);
nood5.setInfo(item5);
ItemList list = new ItemList();
list.insertInfo(item.getName(), item.getTag(), item.getCurrent(), item.getPrice());
list.insertInfo(item2.getName(), item2.getTag(), item2.getCurrent(), item2.getPrice());
list.insertInfo(item3.getName(), item3.getTag(), item3.getCurrent(), item3.getPrice());
list.insertInfo(item4.getName(), item4.getTag(), item4.getCurrent(), item4.getPrice());
list.insertInfo(item5.getName(), item5.getTag(), item5.getCurrent(), item5.getPrice());
list.printAll();
}
}
還有我的輸出...
風火輪自行車
現在,如果我更改5個對象的rfidTags,以使下一個對象大於最后一個對象,則它可以工作,但如果將它們放置成現在的樣子,則不行。
盡量保持簡單。 不要陷入將所有人分為不同的“特殊”情況的陷阱。
public void insertInfo(String name, String rfidTag, String initPosition, double price){
ItemInfo obj = new ItemInfo(name, rfidTag, initPosition, initPosition, price);
ItemInfoNode addition = new ItemInfoNode();
addition.setInfo(obj);
++listCount;
// Walk to the item following:
ItemInfoNode insertionNext = head;
while (insertionNext != null
&& insertionNext.getInfo().getTag().compareTo(rfidTag) >= 0) {
insertionNext = insertionNext.next;
}
ItemInfoNode insertionPrevious = insertionNext == null ? tail
: insertionNext.previous;
// Prepare addition itself:
addition.next = insertionNext;
addition.previous = insertionPrevious;
// The next link backwards should point to the addition:
if (insertionNext == null) {
tail = addition;
} else {
insertionNext.previous = addition;
}
// The previous link forwards should point to the addition:
if (insertPrevious == null) {
head = addition;
} else {
insertPrevious.next = addition;
}
}
首先,我一直被教導鏈表的以下結構
在您的列表類中,您需要以下內容(以sudo代碼顯示)
list class
{
Node head;
Node current;
constructor(Object O){
Node n = new Node(O);
head = n;
current = head;
}
void addItem ( Object o)
{
Node n = new Node(o);
if(head.nextNode() == null)
head.nextNode(n);
else
current.nextNode(n);
current = n;
}
}
從我的示例中可以看到,列表類中應該有兩個Node指針。 一個指向列表的頭,另一個指向當前節點。 添加項目時,首先設置當前項目,然后將其發送到下一個項目。 我認為這是您的問題所在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.