创建一个对象堆栈类 - 我哪里出错了



我仍然是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;
    }
} 
  1. stack似乎打算成为元素类型的数组,push(int)告诉我这是int
  2. 什么样的通用堆栈仅限于int
  3. setCount似乎打算在GeneralStack(int)构造函数中maxCapacity
  4. 您的isEmpty()方法将无法正常工作,因为您在if条件后有一个意外的分号。 if (count == 0) ;的意思是"如果count等于零,则什么都不做"。您可能打算:

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

    实际上,整个方法可以缩短为单个语句。

  5. #3 中的相同问题也适用于 isFull() ,具有类似的缩短形式。您的范围内也没有maxCapacity变量...也许您忘记声明和初始化字段?
  6. 分配给堆栈应该改变与顶部对应的数组元素。
  7. 如果声明为 voidpop()不应返回任何内容。此外,可以使用递减运算符缩短count = count - 1--,例如 --count; .
  8. 大概你的意思是给top()命名(从pop()的代码来看(,你不小心命名topVal().此外,从不在方法作用域中声明topVal变量。您可以通过直接从数组中返回元素来重写该方法以完全避免对变量的需求。

我哪里做错了?

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

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

我同意请使用类似数组列表的东西。 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;
    }
} 

最新更新