2015-07-21 10 views
11

Also ich bin ziemlich neu in Java und Programmierung und ich frage mich, wie man eine Knotenklasse erstellen?Erstellen einer Knotenklasse in Java

Bisher habe ich:

public class ItemInfoNode{ 
    private ItemInfoNode next; 
    private ItemInfoNode prev; 
    private ItemInfo info; 
    public ItemInfoNode(ItemInfo info, ItemInfoNode next, ItemInfoNode prev){ 
     info = info; 
     next = next; 
     prev = prev; 
    } 
    public void setInfo(ItemInfo info){ 
     info = info; 

    } 
    public void setNext(ItemInfoNode node){ 
     next = node; 
    } 
    public void setPrev(ItemInfoNode node){ 
     prev = node; 
    } 
    public ItemInfo getInfo(){ 
     return info; 
    } 
    public ItemInfoNode getNext(){ 
     return next; 
    } 
    public ItemInfoNode getPrev(){ 
     return prev; 
    } 

} 

So ziemlich die Frage für jene Methoden gefragt, so habe ich diese nach unten, aber die nächste Frage stellt mich auf den Kopf und Schwanz von ItemInfoNode Knoten zu verweisen. Nur ein bisschen verwirrt hier. Danke

EDIT: Danke für die Hilfe Jungs! Ich versuche, eine "InsertInfo" -Methode zu erstellen, die Informationen wie Name, Preis, Tag-Nummer usw. in einen Knoten einfügt. Wie gehe ich vor, um diese Methode zu erstellen?

Bisher habe ich das .. ich in einer anderen Klasse einen Iteminfo Konstruktor, der alle diese hat, aber ich bin mir nicht sicher, wie das verwenden,/wenn ich selbst sollte bin ..

public void InsertInfo(String name, String rfdnumber, double price, String original_position){ 

     head = new ItemInfoNode (Iteminfo, head); 
    } 
tun
+1

Die Klasse scheint gut. Die Kopf- und Endknoten sollten in einer anderen Klasse namens LinkedList oder ähnlich definiert sein. –

+0

Wie würde ich diese Klasse erstellen? Genauer gesagt, wie erstelle ich diese Kopf- und Null-Referenzen. – John

+0

Ändern Sie 'info = info;' in 'this.info = info;', dasselbe gilt für den Rest der Felder in Konstruktor und Setter. – Pshemo

Antwort

8

Willkommen in Java! Diese Knoten sind wie Blöcke, sie müssen zusammengesetzt werden, um erstaunliche Dinge zu tun! In diesem speziellen Fall die Knoten eine Liste darstellen kann, eine verknüpfte Liste können Sie sehen, ein Beispiel hier:

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 void addFront(ItemInfo info) { 
     size++; 
     if (head == null) { 
      head = new ItemInfoNode(info, null, null); 
      tail = head; 
     } else { 
      ItemInfoNode node = new ItemInfoNode(info, head, null); 
      this.head.prev = node; 
      this.head = node; 
     } 
    } 

    public ItemInfo removeBack() { 
     ItemInfo result = null; 
     if (head != null) { 
      size--; 
      result = tail.info; 
      if (tail.prev != null) { 
       tail.prev.next = null; 
       tail = tail.prev; 
      } else { 
       head = null; 
       tail = null; 
      } 
     } 
     return result; 
    } 

    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 class ItemInfoNode { 

     private ItemInfoNode next; 
     private ItemInfoNode prev; 
     private ItemInfo info; 

     public ItemInfoNode(ItemInfo info, ItemInfoNode next, ItemInfoNode prev) { 
      this.info = info; 
      this.next = next; 
      this.prev = prev; 
     } 

     public void setInfo(ItemInfo info) { 
      this.info = info; 
     } 

     public void setNext(ItemInfoNode node) { 
      next = node; 
     } 

     public void setPrev(ItemInfoNode node) { 
      prev = node; 
     } 

     public ItemInfo getInfo() { 
      return info; 
     } 

     public ItemInfoNode getNext() { 
      return next; 
     } 

     public ItemInfoNode getPrev() { 
      return prev; 
     } 
    } 
} 

EDIT:

ItemInfo wie dies erklären:

public class ItemInfo { 
    private String name; 
    private String rfdNumber; 
    private double price; 
    private String originalPosition; 

    public ItemInfo(){ 
    } 

    public ItemInfo(String name, String rfdNumber, double price, String originalPosition) { 
     this.name = name; 
     this.rfdNumber = rfdNumber; 
     this.price = price; 
     this.originalPosition = originalPosition; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public String getRfdNumber() { 
     return rfdNumber; 
    } 

    public void setRfdNumber(String rfdNumber) { 
     this.rfdNumber = rfdNumber; 
    } 

    public double getPrice() { 
     return price; 
    } 

    public void setPrice(double price) { 
     this.price = price; 
    } 

    public String getOriginalPosition() { 
     return originalPosition; 
    } 

    public void setOriginalPosition(String originalPosition) { 
     this.originalPosition = originalPosition; 
    } 
} 

Dann , Sie können Ihre Knoten in der verketteten Liste wie folgt verwenden:

public static void main(String[] args) { 
    ItemLinkedList list = new ItemLinkedList(); 
    for (int i = 1; i <= 10; i++) { 
     list.addBack(new ItemInfo("name-"+i, "rfd"+i, i, String.valueOf(i))); 

    } 
    while (list.size() > 0){ 
     System.out.println(list.removeFront().getName()); 
    } 
} 
+0

Vielen Dank! – John

+0

@John Gern geschehen! –

Verwandte Themen