简体   繁体   English

在通用堆栈类中创建通用堆栈

[英]Creating a generic stack in generic stack class

I'm trying to teach myself some java and im stuck on a problem that seems kind of easy but i still don't seem to find a solution. 我试图自学一些Java,并且遇到了似乎很简单的问题,但是我仍然找不到解决方案。

What I have so far: 到目前为止,我有:

Interface: 接口:

public interface ADTStack<T> {


public boolean isEmpty();


public void push(T element);


public T top() throws IllegalStateException;


public void pop() throws IllegalStateException;
}

Class Stack: 类堆栈:

public class Stack<T> implements ADTStack<T> {

private java.util.LinkedList<T> data;  



public Stack() {
    data = new java.util.LinkedList<T>();
}

@Override
public boolean isEmpty() {
    return data.isEmpty();
}

@Override
public void push(T element) {
    data.add(0, element);
}

@Override
public T top() throws IllegalStateException {
    if (isEmpty()) {
        throw new IllegalStateException("Stack is emtpy.");
    }
    return data.getFirst();
}

@Override
public void pop() throws IllegalStateException {
    if (isEmpty()) {
        throw new IllegalStateException("Stack is empty.");
    }
    data.remove(0);
}

Alright , so here is what I'm trying to do. 好了,这就是我想要做的。 I'm trying to write a methode equals to compare two Stacks. 我试图写一个方法equals比较两个堆栈。 My idea was to use a third Stack to be able to bring both stacks into their original state after comparing them. 我的想法是使用第三个堆栈,以便在比较它们之后将两个堆栈都恢复为原始状态。

Here's what I have: 这是我所拥有的:

    Stack supportStack = new Stack();

public boolean equals(ADTStack<T> s){
    if (data.isEmpty() != s.isEmpty()){         
        return false;
    }
    if (data.isEmpty() && s.isEmpty()){     
        return true;
    }

    T element_a  =  this.top();             
    T element_b  = s.top();


    if( (element_a ==null && (element_b !=null) || !element_a.equals(element_b) || element_a != null && element_b == null)){
        return false;
    }

    data.pop();
    s.pop();                        
    supportStack.push(element_a);       
    boolean result = data.equals(s);    

    while (!supportStack.isEmpty()){        
        data.push(supportStack.top());   
        s.push(supportStack.top());
        supportStack.pop();
    }
    return result;                      
}

I get a lot of errors when I compile the code and it seems that something is wrong with : 编译代码时出现很多错误,似乎有些问题:

Stack supportStack = new Stack();

I don't really know what's wrong and how to solve the error. 我真的不知道怎么了以及如何解决错误。 I made a runner-class and I tried the constructor and it worked so I'm confused at what's wrong. 我做了一个跑步者班,尝试了构造函数,它起作用了,所以我对什么地方感到困惑。

public class Runner {

   public static void main(String[] args){
      Stack test = new Stack();
      test.push(12);
      System.out.println(test.top());
   }
}

I gladly take any advice or constructive criticism since I'm teaching myself and if anything seems unclear feel free to ask. 自从我自学以来,我很乐意接受任何建议或建设性的批评,如果有什么不清楚的地方,请随时提出。

Stack supportStack = new Stack();

Stack is called a raw type : it's like not using generics. Stack被称为原始类型 :就像不使用泛型一样。 You need to use: 您需要使用:

Stack<T> supportStack = new Stack<T>();

But, as a hint: you don't need to do this. 但是,提示:您不需要这样做。 You can just do: 您可以这样做:

return this.data.equals( s.data );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM