从链表类中调用方法

时间:2014-05-28 05:39:30

标签: java reference static linked-list

public class Bank {


public static void nsUI(){
    Bank objBank = new Bank();
    objBank.LoadData();
}



public void UI(){


    if(UserInput==7){
        Exit();

    }

}






public void Exit(){
System.out.printf("Have a Wonderful Day! %n");
IntSLList.printAll();   /////////This is where the problem is
//  WriteData();
//System.exit(0);
}



public static void main(String[] args) {
    nsUI();
 }


    }








**This is the Linked list file**

//************************  IntSLList.java  **************************
//           singly-linked list class to store integers

public class IntSLList {
protected IntSLLNode head, tail;
public static void createInst(){
    new IntSLList().printAll();

}



public IntSLList() {
    head = tail = null;
}
public boolean isEmpty() {
    return head == null;
}
public void addToHead(String AN) {
    head = new IntSLLNode(AN,head);
    if (tail == null) // checks for empty list
        tail = head;
}
public void addToTail(String AN, Double AB) {
    if (!isEmpty()) {
        tail.next = new IntSLLNode(AN,AB);
        tail = tail.next;
    }
    else 
        head = tail = new IntSLLNode(AN,AB);
}
public String deleteFromHead() { // delete the head and return its info; 
    String AN = head.AccountNumber;
    if (head == tail)    // if only one node on the list;
         head = tail = null;
    else head = head.next;
    return AN;
}
public String deleteFromTail() { // delete the tail and return its info;
    String AN = tail.AccountNumber;
    if (head == tail)    // if only one node in the list;
         head = tail = null;
    else {               // if more than one node in the list,
         IntSLLNode tmp;    // find the predecessor of tail;
         for (tmp = head; tmp.next != tail; tmp = tmp.next);
         tail = tmp;     // the predecessor of tail becomes tail;
         tail.next = null;
    }
    return AN;
}
public void printAll() {
    for (IntSLLNode tmp = head; tmp != null; tmp = tmp.next)
        System.out.print(tmp.AccountNumber + " " +tmp.AccountBalance+"");                
}
public boolean isInList(String AN) {
    IntSLLNode tmp;
    for (tmp = head; tmp != null && tmp.AccountNumber != AN; tmp = tmp.next);
    return tmp != null;
}
public void delete(String AN){  // delete the node with an element el;
    if (!isEmpty())
        if (head == tail && AN == head.AccountNumber) // if only one
             head = tail = null;             // node on the list;
        else if (AN == head.AccountNumber) // if more than one node on the list;
             head = head.next;    // and el is in the head node;
        else {                    // if more than one node in the list
             IntSLLNode pred, tmp;   // and el is in a non-head node;
             for (pred = head, tmp = head.next;  
                  tmp != null && tmp.AccountNumber != AN; 
                  pred = pred.next, tmp = tmp.next);
             if (tmp != null) {   // if el was found;
                 pred.next = tmp.next;
                 if (tmp == tail) // if el is in the last node;
                    tail = pred;
             }
        }
    }
}

Eclipse对我大吼大叫说我无法对非静态方法进行静态引用,但是我应该怎样做...我读到我需要创建一个IntSLL实例上课,我试过了,但我仍然无法让它发挥作用......我想我做错了。

PS,请不要烧我格式化这篇文章。这是我第一次在这里发布,通常我可以根据有类似问题的人来解决我的问题,以及在这里提供不同想法的令人敬畏的社区。

PPS,我知道我不需要在Exit()的方法中使用printAll方法,我试图弄清楚我需要做些什么来让我的程序通过我的程序步骤步骤实现链表而不是线性搜索...最终线性搜索将被链表的实现所取代。

谢谢大家!

1 个答案:

答案 0 :(得分:0)

首先,您在银行类中调用IntSLList方法,但您从未创建过列表。我认为您需要在银行类中创建一个列表实例。例如:

public class Bank {
private myList = new IntSLList();
//rest of your code
}

然后,调用myList.printAll()而不是调用IntSLList.printAll()。我希望这是有道理的,我可能会错误地解释你的实现并且如果我这样做而道歉,但我相信这就是你的问题所在。如果我是正确的,那么您还应该将数据添加到您的银行类别的列表中。希望这可以帮助!祝你好运!