java - 为什么我的堆栈只添加一个元素然后又删除它?

标签 java stack

我已经调试了代码并发现它添加了第一个元素(堆栈大小= 1) 但是当尝试添加下一个元素时,堆栈大小跳回到 0

我认为问题出在公共(public)无效转换中。

public void convert(int N) {
    this.N = N;
    int binary[] = new int[10];
    int index = 0;
    while (N > 0) {
        binary[index++] = N % 2;
        N = N / 2;
    }
    for (int x = index - 1; x >= 0; x--) { 
        this.binStack.push(binary[x]);        //Here is the Problem
    }
}
<小时/>

这是整个代码:

import java.util.Stack;

public class Dec2Bin {
    public Stack<Integer> binStack;  // We make it public to modify it in our tests.
    private int N;

    /**
     * Constructor of an empty object. Use method {@code convert()} to convert a number.
     */
    public Dec2Bin() {
        binStack = new Stack<>();
    }

    /**
     * Returns the number that is converted as {@code int}.
     *
     * @return the converted number
     */
    public int getN() {
        return N;
    }

    /**
     * Converts the given number into binary format, with each digit being represented in a
     * stack of {@code int}.
     *
     * @param N the number that is to be converted.
     */
    public void convert(int N) {
        // TODO implement this method
        this.N = N;
        int binary[] = new int[10];
        int index = 0;
        while (N > 0) {
            binary[index++] = N % 2;
            N = N / 2;
        }
        for (int x = index - 1; x >= 0; x--) {
            this.binStack.push(binary[x]);
        }
    }

    /**
     * Returns the digits that are stored in {@code binStack} as a string. To is the binary format of the
     * converted number.
     * For testing purpose, we require that the function works also, if the variable {@code binStack} is
     * modified externally.
     *
     * @return a string representation of the number in binary format.
     */
    @Override
    public String toString() {
        // Caution: Stack.toString() does NOT respect stack order. Do not use it.
        // TODO implement this method
        String finalBinär = new String();
        for (int y = 0; y < binStack.size(); y++){
            String binär = Integer.toString(binStack.pop());
            String prevBinär = new String();
            finalBinär = prevBinär.concat(binär);
        }
        return finalBinär;
    }

    public static void main(String[] args) {
        Dec2Bin dec2bin = new Dec2Bin();
        dec2bin.convert(50);
        System.out.println("Die Zahl " + dec2bin.getN() + " in Binärdarstellung: " + dec2bin);
        // Do it another time to demonstrate that toString does not erase the binStack.
        System.out.println("Die Zahl " + dec2bin.getN() + " in Binärdarstellung: " + dec2bin);
    }
}

最佳答案

´´´
import java.util.Stack;

public class Dec2Bin {
    public Stack<Integer> binStack;  // We make it public to modify it in our tests.
    private int N;

    /**
     * Constructor of an empty object. Use method {@code convert()} to convert a number.
     */
    public Dec2Bin() {
        binStack = new Stack<>();
    }

    /**
     * Returns the number that is converted as {@code int}.
     *
     * @return the converted number
     */
    public int getN() {
        return N;
    }

    /**
     * Converts the given number into binary format, with each digit being represented in a
     * stack of {@code int}.
     *
     * @param N the number that is to be converted.
     */
    public void convert(int N) {
        // TODO implement this method
        this.N = N;
        int binary[] = new int[10];
        int index = 0;
        while (N > 0) {
            binary[index++] = N % 2;
            N = N / 2;
        }
        for (int x = index - 1; x >= 0; x--) {
            this.binStack.push(binary[x]);
        }
    }

    /**
     * Returns the digits that are stored in {@code binStack} as a string. To is the binary format of the
     * converted number.
     * For testing purpose, we require that the function works also, if the variable {@code binStack} is
     * modified externally.
     *
     * @return a string representation of the number in binary format.
     */
    @Override
    public String toString() {
        // Caution: Stack.toString() does NOT respect stack order. Do not use it.
        // TODO implement this method
        String finalBinär = new String();
        for (int y = 0; y < binStack.size(); y++){
            String binär = Integer.toString(binStack.pop());
            String prevBinär = new String();
            finalBinär = prevBinär.concat(binär);
        }
        return finalBinär;
    }

    public static void main(String[] args) {
        Dec2Bin dec2bin = new Dec2Bin();
        dec2bin.convert(50);
        System.out.println("Die Zahl " + dec2bin.getN() + " in Binärdarstellung: " + dec2bin);
        // Do it another time to demonstrate that toString does not erase the binStack.
        System.out.println("Die Zahl " + dec2bin.getN() + " in Binärdarstellung: " + dec2bin);
    }
}

´´´

关于java - 为什么我的堆栈只添加一个元素然后又删除它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61733126/

相关文章:

java - 在 Spring 中定义 @ManyToOne 关系后,如何访问底层列?

java - 正则表达式匹配字符串列表并忽略字符串列表

c - 用c构建堆栈的问题

java - 需要说明以减少堆大小

python-3.x - pop() 和 enumerate() 如何交互的问题

java - 有没有办法在堆栈中搜索带有 "any"参数的内容?

c++ - 如何查找lua堆栈中有多少项(值)

java - LWJGL OpenGL使用纹理图集UV坐标绘制文本

java - 许多简单类实例,我的做法是最好的解决方案吗?

java - 在java中,如何从日志消息中获取字符串(日期)