2013-10-06 47 views
-2

關於我應該傳入該推送方法的問題。我想將一系列.txt文件中的信息推送到我的主方法內的維基堆棧中,以便稍後可以彈出並使用它。以下是錯誤NetBeans是給我:推送到在java中使用鏈接列表的堆棧

發現推()方法 FancyStack.push(FancyStack>)沒有合適的方法是不適用 (實際的和正式的參數列表的長度不同) 方法FancyStack.push(節點)不適用 (實際和正式參數列表長度不同)

請讓我知道是否有人想要其他信息。

FancyStack<Node<WikiObjects>> wikis = new FancyStack<Node<WikiObjects>>(); 
FancyStack<Node<WikiObjects>> titles = new FancyStack<Node<WikiObjects>>(); 
WikiEdits edits = new WikiEdits(args); 
String titleName = edits.title(); 
String editID = edits.editID(); 


    while(edits.moveToNext() != false){ 

     wikis.push(); 
     } 

在此先感謝!

編輯:這是我的FancyStack

import java.util.*; 

public class FancyStack<E> { 

//pieced together linked list 
private int cnt; 
private Node<E> head; 
public FancyStack<E> stack; 
public FancyStack<E> s; 

public FancyStack() { 
    head = null; 
    cnt = 0; 
} 

public void push(E item) { //THIS WORKS 
    //your average kind of pushing an item onto stack 
    Node<E> newNode = new Node<E>(item); 
    newNode.setLink(head); 
    head = newNode; 
    cnt++; 
} 

public void push(FancyStack<E> s) { //THIS WORKS 
    //pushes all elements of FancyStack s into a new stack (this) 
    //however stack this is in reverse order from FancyStack<E> s 
    if (s.isEmpty()) { 
     throw new NoSuchElementException("Empty Stack"); 
    } 

    while (!s.isEmpty()) { 
     Node<E> element = s.head; 
     this.push(element.getInfo()); 
     s.pop(); 
     element = element.getLink(); 
    } 

} 

public boolean isEmpty() { 
    return head == null; 
} 

public int size() { 
    return cnt; 
} 

public E pop() { //THIS CORRECT 
    if (isEmpty()) { 
     throw new NoSuchElementException("Stack underflow"); 
    } else { 
     E item = head.item; 
     head = head.link; 
     cnt--; 
     return item; 
    } 
} 

public E peek() { //THIS WORKS 
    if (isEmpty()) { 
     throw new NoSuchElementException("Stack underflow"); 
    } 
    return head.item; 
} 

public FancyStack<E> reversed() { 
    /* if (this.isEmpty()) { // want to test exceotion error with this commented out. 
     throw new NoSuchElementException("Empty Stack"); 
    }*/ 
    FancyStack<E> newthis = new FancyStack<E>(); 
    while (!this.isEmpty()) { //elmt short for 'element' 
     Node<E> elmt = this.head; 
     newthis.push(elmt.getInfo()); 
     this.pop(); 
     elmt = elmt.getLink(); 
    } 
    return newthis; 

} 

}

回答

1

你指定wikisNode<WikiObjects>FancyStack對象。您必須按一個Node<WikiObjects>對象。 FancyStack也可以讓你推另一個FancyStack。在我看來,這是一個命名不佳的API,因爲它沒有做它所說的。它不是推動FancyStack而是推動FancyStack的所有元素。它應該被命名爲pushAll

基於提供FancyStack源

+0

我上述編輯成包括FancyStack – Talaria

+0

有道理編輯。謝謝! – Talaria