Java实例变量不保留状态。

huangapple go评论72阅读模式
英文:

Java Instance Variable does not retain state

问题

import java.util.NoSuchElementException;

public class Stack<T> {

    private Node first;
    private Node current;
    private int N;

    private class Node {
        T item;
        Node next;
    }

    public void push(T item) {
        Node oldFirst = first;

        Node first = new Node();
        first.item = item;
        first.next = oldFirst;

        N++;
    }

    public T pop() throws NoSuchElementException {
        try {
            T item = first.item;
            first = first.next;
            N--;
            return item;
        } catch (java.lang.NullPointerException error) {
            throw new NoSuchElementException("Stack is empty.");
        }
    }
}

Test Client:

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class TestStack {

    /**
     * Test the LIFO property of the stack i.e. the item pushed last into the
     * stack is the one to be popped first from the stack.
     */
    @Test
    void testLIFOPropertyWithIntegerStack() {
        int[] testClient = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        Stack<Integer> stack = new Stack<>();

        for (int item : testClient) {
            stack.push(item);
        }

        int index = testClient.length - 1;
        while (stack.size() != 0) {
            int item = stack.pop();
            assertEquals(item, testClient[index--]);
        }
    }
}
英文:

I have a straightforward implementation of stack, using Linked Lists, however during testing the code I noticed that the instance variable first does not retain state and gets reverted to NULL during subsequent push operations. While N retains its values, the variable first does not. Can someone please help?

import java.util.NoSuchElementException;

public class Stack &lt;T&gt; {

    private Node first;
    private Node current;
    private int N;

    private class Node {
        T item;
        Node next;
    }

    public void push(T item) {
        Node oldFirst = first;

        Node first = new Node();
        first.item = item;
        first.next = oldFirst;

        N++;
    }

    public T pop()  throws NoSuchElementException{
        try {
            T item = first.item;
            first = first.next;
            N--;
            return item;
        } catch (java.lang.NullPointerException error) {
            throw new NoSuchElementException(&quot;Stack is empty.&quot;);
        }
    }

}

Test Client:


import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class TestStack {

   /**
     * Test the LIFO property of the stack i.e. the item pushed last into the
     * stack is the one to be popped first from the stack.
     */
    @Test
    void testLIFOPropertyWithIntegerStack() {
        int[] testClient = new int[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        Stack&lt;Integer&gt; stack = new Stack&lt;&gt; ();

        for (int item : testClient) {
            stack.push(item);
        }

        int index = testClient.length - 1;
        while (stack.size() != 0) {
            int item = stack.pop();
            assertEquals(item, testClient[index--]);
        }
    }

}

答案1

得分: 1

在你的push方法中存在一个问题。你的 Node first = new Node(); 正在隐藏你的堆栈中的字段 first

所以,请修改为:

// 这是你的对象字段
first = new Node();

这样测试将会通过。记住你需要实现 size() 方法,因为你的测试方法在使用它。

public int size(){
    return N;
}
英文:

There is a problem in your push method. Your Node first = new Node(); is hiding the field first in your stack.

So, change:

Node first = new Node();

to

// This is your object field
first = new Node();

And the test will pass. Remember that you need to implement size() since your test method uses it.

public int size(){
    return N;
}

huangapple
  • 本文由 发表于 2020年9月24日 23:40:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/64049838.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定