【问题标题】:Reference Value Copy参考值副本
【发布时间】:2013-10-27 17:36:30
【问题描述】:

我在 JAVA 中动态地实现了一个堆栈,我遇到了这个问题。问题是,我的代码有效,但我不知道为什么。所以这是我不明白的部分代码:

  Node<E> newNode = new Node(elem,top);
  newNode=top;
  size++;

所以,我的 newNode 的第二个参数是它旁边的对象,在这种情况下是顶部。 然后我说我的 newNode=top;所以,按照我的逻辑,newNode 在 newNode 旁边,因为我在之后的指令中说过 newNode = top;我在这里想念什么?我知道这是一个愚蠢的问题 :( 问题是,它有效,而且我看到了一些类似的实现,我只是不明白它为什么有效。

编辑:我会发布我的整个代码:

类节点:

public class Node<E> {
    private E element;
    private Node<E> next;

    public Node(E element, Node<E> next) {
        this.element = element;
        this.next = next;
    }

    public E getElement() {
        return element;
    }

    public void setElement(E element) {
        this.element = element;
    }

    public Node<E> getNext() {
        return next;
    }

    public void setNext(Node<E> next) {
        this.next = next;
    }

}

//到此结束

接口栈:

public interface Stack<E>  {

    //numero de elementos presentes na pilha 
    public int size( ); 
    //nao contem elementos?
    public boolean isEmpty( ); 
    //devolve proximo objecto a sair, sem remover
    public E peek( )    
        throws EmptyStackException; 

    //empilha novo elemento
    public void push(E o)   
        throws FullStackException;
    //desempilha proximo elemento                                                                                     
    public E pop()          
        throws EmptyStackException;     
}

//接口到此结束

类堆栈动态

public class StackDynamic<E> implements Stack<E>{


    private int size;
    private Node<E> top;
    private int maxCapacity;


    public StackDynamic(int capacity)
    {
      this.maxCapacity=capacity;
      this.size=0;
      this.top=null;
    }

    public StackDynamic()
    {
      this(-1);

    }

    @Override
    public int size() {
        return this.size;
}

    @Override
    public boolean isEmpty() {
       return (this.size == 0);

    }

    @Override
    public E peek() throws EmptyStackException {
        if (isEmpty()) {
            throw new EmptyStackException("A pilha está vazia.");
        }
        return top.getElement();

    }

    @Override
    public void push(E elem) throws FullStackException {
    if(size==maxCapacity){
        throw new FullStackException("Está cheio");
    }
    **Node<E> newNode = new Node<>(elem, top);


    top=newNode;
    size++;**  //error here
    }

    @Override
    public E pop() throws EmptyStackException {
        if (isEmpty()) {
            throw new EmptyStackException("A pilha está vazia.");
        }

         E elemRemoved = top.getElement();
         top = top.getNext();
         size--;
         return elemRemoved;




    }

//课程到此结束

现在 newNode=top;命令对我来说没有多大意义:S

【问题讨论】:

  • 你确定你没有把第二个语句的顺序搞混了吗?我希望它是top = newNode;
  • 我想我们需要更多代码。

标签: java reference linked-list copy stack


【解决方案1】:

它很可能“有效”,因为

  • 代码不是你说的那样(虽然是我的第一个,但我忽略了它)
  • 其他地方的代码隐藏了错误
  • 您没有正确测试代码。

我怀疑如果您删除 newNode=top; 行,它仍然会“工作”

【讨论】:

  • @PedroCosta 我找不到您要询问的行,因为您引用的代码是正确的。您能帮我突出显示不正确的行吗?
  • done mate :D 它在类堆栈动态的方法推送上,我把代码放在 BOLD 上。
  • @PedroCosta 我还是看不到newNode=top;,我只能看到top=newNode;
  • 是的,就是这样,top=newNode。如果我说 top=newNode,新节点指向自己,不是吗? =S
  • @PedroCosta 否。top 是一个指向旧值top 的节点我建议您在调试器中逐步执行代码,您将能够看到发生了什么。
猜你喜欢
  • 2013-12-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-03
  • 1970-01-01
  • 1970-01-01
  • 2018-09-03
  • 2019-11-29
相关资源
最近更新 更多