2013-10-19 32 views
-7

所以我有一些Set,我想將元素添加到LinkedList,以便我可以遍歷它們。我將如何去在java中這樣做?將設置添加到java中的迭代列表中

+2

的代碼。與代碼。或互聯網搜索,然後編碼。 –

+0

'puslic static void main(String [] args)' – Tdorno

回答

0

從這裏的一個問題摘自:Adding items to end of linked list

class Node { 
    Object data; 
    Node next; 
    Node(Object d,Node n) { 
     data = d ; 
     next = n ; 
     } 

    public static Node addLast(Node header, Object x) { 
     // save the reference to the header so we can return it. 
     Node ret = header; 
    // check base case, header is null. 
    if (header == null) { 
     return new Node(x, null); 
    } 

    // loop until we find the end of the list 
    while ((header.next != null)) { 
     header = header.next; 
    } 

    // set the new node to the Object x, next will be null. 
    header.next = new Node(x, null); 
    return ret; 
    } 
}