0

我还是 Java 新手,我正在尝试创建一个使用对象数组的通用堆栈队列。我知道我做错了,即声明数组并分配数组长度。

有人可以看看并给我一些反馈吗?

public class GeneralStack
{
    GeneralStack [] stack;  //not sure how to declare this
    private int count;
    private static final int DEFAULT_CAPACITY = 100;

//default constructor
public GeneralStack()
    {
        stack = new int[DEFAULT_CAPACITY];
        count = 0;
    }

//alternate constructor
public GeneralStack (int maxCapacity)
    {
        stack = new int[maxCapacity];
        count = setCount;
    }

//accessor getCount
public int getCount ()
    {
    return count;
    }

//accessor isEmpty
public boolean isEmpty ()
    {
    boolean isEmpty=false;
    if (count == 0);
        {
            isEmpty=true;
        }
    return isEmpty;
    }

//accessor isFull
public boolean isFull ()
    {
    boolean isFull=false;
    if (count == maxCapacity);
        {
        isFull=true;
        }
    return isFull;
    }

//mutator push
public void push (int value)
    {
    if (isFull ())
        {
        throw new IllegalArgumentException("Stack is full");
        }
    else
        {
        stack[value]; //not sure how to assign value to the stack
        count++;
        }
    }

//mutator pop
public void pop ()
    {
         int topVal = top();
        count = count-1;
        return topVal;
    }

//accessor top
public int topVal ()
    {
    if (isEmpty())
        {
        throw new IllegalArgumentException("Stack is empty");
        }
    else 
        {
        topVal=stack[count-1];
        }
    return topVal;
    }
} 
4

3 回答 3

2
  1. stack似乎打算成为元素类型的数组,它push(int)告诉我是int.
  2. 什么样的通用栈仅限于int
  3. setCount似乎打算maxCapacityGeneralStack(int)构造函数中。
  4. 您的isEmpty()方法将无法正常工作,因为您的if条件后面有一个意外的分号。if (count == 0) ;意思是“如果count等于零,什么也不做”。您可能打算:

    if (count == 0) {
      isEmpty = true;
    }
    

    事实上,整个方法可以缩短为一条语句。

  5. #3 中的相同问题也适用于isFull(),具有类似的缩写形式。您在范围内也没有maxCapacity变量......也许您忘记声明和初始化一个字段?
  6. 分配给堆栈应该是改变与top对应的数组元素。
  7. pop()如果已声明,则不应返回任何内容void。此外,count = count - 1可以使用减量运算符来缩短,--例如--count;
  8. 大概你打算命名的东西top()(根据 的代码判断pop()),你不小心命名了topVal(). 此外,您永远不会topVal在方法范围内声明变量。您可以通过直接从数组中返回元素来重写该方法以完全消除对变量的需求。
于 2012-08-07T05:12:13.797 回答
1

我哪里做错了?

  1. 在我看来,这里有一些编译错误。例如,您调用了一个top()似乎不存在的函数(我猜您的意思是topVal())。
  2. 如果您希望您的堆栈采用任何类型,您应该查看Generic Types
  3. 在堆栈中,您需要接受与push返回的参数相同的类型pop。您的push函数接受整数,但您的pop函数是无效的。
  4. 支持您的数据结构的数组需要与上面步骤 (2) 中使用的类型相同。就像现在一样,您似乎希望它是一个整数,但正如步骤 (1) 所建议的那样,也许可以考虑使用泛型类型。
  5. 在你的push函数中,你说“不确定如何为堆栈赋值”,你应该说stack[count] = value.

这些事情应该让您开始寻找可行的解决方案。可能还有其他一些事情需要改变,但这些至少是你在这里犯的一些基本错误。

于 2012-08-07T05:12:15.233 回答
-1

我同意请使用像 arraylist 这样的东西。http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Stack.html 已经实现了类,但我修复了你的代码,以防你是java新手并且只是在玩它. 没有检查逻辑,但希望语法更正有帮助

    package Temp;

    public class GeneralStack
{
    int[] stack;  //not sure how to declare this
    private int count;
    private int maxCapacity;
    private static final int DEFAULT_CAPACITY = 100;

//default constructor
public GeneralStack()
    {
        stack = new int[DEFAULT_CAPACITY];
        count = 0;
        maxCapacity = this.DEFAULT_CAPACITY;
    }

//alternate constructor
public GeneralStack (int maxCapacity)
    {
        stack = new int[maxCapacity];
        count = 0;
        this.maxCapacity = maxCapacity;
    }

//accessor getCount
public int getCount ()
    {
    return count;
    }

//accessor isEmpty
public boolean isEmpty ()
    {
    boolean isEmpty=false;
    if (count == 0);
        {
            isEmpty=true;
        }
    return isEmpty;
    }

//accessor isFull
public boolean isFull ()
    {
    boolean isFull=false;
    if (count == maxCapacity);
        {
        isFull=true;
        }
    return isFull;
    }

//mutator push
public void push (int value)
    {
    if (isFull ())
        {
        throw new IllegalArgumentException("Stack is full");
        }
    else
        {
        stack[count] = value; //not sure how to assign value to the stack
        count++;
        }
    }

// you cant return value from void function so changing it to int return you can ignore a return value 
public int pop ()
    {
         int topVal = topVal();
        count = count-1;
        return topVal;
    }

//accessor top
public int topVal ()
    {
    int topVal;
    if (isEmpty())
        {
        throw new IllegalArgumentException("Stack is empty");
        }
    else 
        {
        topVal=stack[count-1];
        }
    return topVal;
    }
} 
于 2012-08-07T05:22:13.570 回答