2013-11-28 52 views
0

我正在進行練習,並遇到問題。在java中的ArrayList中追加對象

NodeList,創建一個static methodNode arrayToNode(String[] arr) 其中字符串數組轉換成列表。你的方法應該創建第一個Node, ,然後再遍歷數組的其餘部分,在每一步創建一個Node,並使用 append將創建的節點放在列表的末尾。在 命令行參數上測試此方法。如果array爲空,會發生什麼情況?

目前我的代碼是這樣的

public static Node arrayToNode(String[] arr) { 
    Node first = new Node(arr[0]); 
    ArrayList<Node> list = new ArrayList<Node>(); 
    for(int i=1; i<arr.length;i++){ 
     list.add(new Node(arr[i])); 
    } 
} 

,你可以看到有沒有return語句呢。 我不確定編寫練習的人是否寫錯Node而不是void,但我不能問他。

append方法是

public void append(Node fin){ 
    if(next==null) 
     next=fin; 
    else 
     append(next); 
} 

和實例變量和構造函數如下:

public String value; 
public Node next; 

public Node(String s){ 
    value =s; 
    next=null; 
} 

我完全不清楚這意味着什麼把節點在列表的末尾因爲ArrayList不斷擴大。 此外,我有關於如何使用它在TestNode類中使用部署append方法的問題。


感謝您的評論。 我現在已經意識到問題所在,並做出了適當的修改。

public static Node arrayToNode(String[] arr){ 
    Node first = new Node(arr[0]); 
    for(int i=1; i<arr.length;i++){ 
     Node nd = new Node(arr[i]); 
     nd.append(nd); 
     first.next=nd; 
    } 

    return first; 
} 

您能否看到這是否正確?

+2

我很確定你的任務並不想讓你創建一個新的ArrayList,而是創建一個鏈接到'下一個節點'的Node對象鏈,如果你有一個'Node',你可以沿着'.next()'方法獲取整個列表內容。 –

+0

那麼編譯器如何允許它既然你已經提到了返回類型作爲Node,但是你在此時沒有返回任何東西,那麼compliler應該給出錯誤 – Deepak

回答

0

除非我失去了一些東西,你的append方法應該是這樣的:

public void append(Node fin) { 
    if (next == null) 
     next = fin; 
    else 
     next.append(fin); // <- this line changed 
} 

這將追加fin向下行,直到到達列表的末尾,而你必須在你的OP途中會給無限遞歸。

如果它應該是這樣,那麼創建列表非常簡單。您可以將每個值附加到原始值。

public class Node { 
    public static void main(String[] args) { 
     Node begin = arrToLL(new String[] { 
      "hello 1", "hello 2", "hello 3", "hello 4", "hello 5" 
     }); 

     while (begin != null) { 
      System.out.println(begin.val); 
      begin = begin.next; 
     } 
    } 

    static Node arrToLL(String[] arr) { 
     if (arr == null) { 
      return null; 
     } else if (arr.length == 0) { 
      return new Node("null"); 
     } 

     int ind = 0; 
     Node begin = new Node(arr[ind++]); 

     while (ind < arr.length) { 
      begin.append(new Node(arr[ind++])); 
     } 

     return begin; 
    } 

    /* instance */ 

    String val; 
    Node next; 

    Node(String val) { this.val = val; } 

    void append(Node ap) { 
     if (next == null) { 
      next = ap; 
     } else { 
      next.append(ap); 
     } 
    } 
} 

輸出是:

hello 1 
hello 2 
hello 3 
hello 4 
hello 5 

裏面的「列出」循環中,您還可以通過像我的println循環分配next給一個變量「提前洗牌」。這樣,你沒有利用「傳遞」。

public static Node arrayToNode(String[] arr){ 
    Node first = new Node(arr[0]); 
    for(int i=1; i<arr.length;i++){ 
     Node nd = new Node(arr[i]); 
     nd.append(nd); // <- appends nd to itself 
     first.next=nd; // <- always assigns the new value to first 
    } 

    return first; 
} 

越來越近了,但我已經評論了err中的兩行。我想你會最終得到的是:

  • 第一個節點與鏈接到
  • 第二節點與鏈接到
  • 本身(第二個節點)
最後一個數組元素的第一個數組元素

你可以做到這一點沒有append但你需要另一個變量洗牌:

public static Node arrayToNode(String[] arr){ 
    Node first = new Node(arr[0]); 
    Node current = first; 

    for(int i=1; i<arr.length;i++){ 
     Node nd = new Node(arr[i]); 

     current.next = nd; // <- append the new node to the last 
     current = nd; // <- shuffle ahead to the new one 
    } 

    return first; 
} 

否則,如果我正確地認爲追加有一個錯誤,你可以做一些更接近我的主要例子(包括如果你想要的洗牌和for循環的作品也是如此)。