Java 创建对象堆栈类-哪里出错了?
我还是Java新手,我正在尝试创建一个使用对象数组的通用堆栈队列。我知道我做得不对,即声明数组和分配数组长度 有人能看一下并给我一些反馈吗Java 创建对象堆栈类-哪里出错了?,java,arrays,object,Java,Arrays,Object,我还是Java新手,我正在尝试创建一个使用对象数组的通用堆栈队列。我知道我做得不对,即声明数组和分配数组长度 有人能看一下并给我一些反馈吗 public class GeneralStack { GeneralStack [] stack; //not sure how to declare this private int count; private static final int DEFAULT_CAPACITY = 100; //default constr
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;
}
}
stack
似乎是元素类型的数组,它告诉我是int
int
setCount
似乎是在GeneralStack(int)
构造函数中设置为maxCapacity
isEmpty()
方法将无法正常工作,因为在if
条件后有一个意外的分号<代码>如果(计数==0)表示“如果count
等于零,则什么也不做”。您可能打算:
if (count == 0) {
isEmpty = true;
}
事实上,整个方法可以缩短为一条语句isFull()
,具有类似的缩写形式。您的作用域中也没有maxCapacity
变量。。。也许您忘记声明和初始化字段了pop()
如果声明为void
,则不应返回任何内容。此外,count=count-1
可以使用减量运算符--
缩短,例如--count代码>
top()
(根据pop()
的代码判断),您无意中命名了topVal()
。此外,您从不在方法范围中声明topVal
变量。通过直接从数组返回元素,您可以重写该方法以完全消除对变量的需要top()
(我猜您的意思是topVal()
)push
返回的pop
参数相同的类型。您的push
函数接受整数,但您的pop
函数无效push
函数中,您说“不确定如何为堆栈赋值”,您应该说stack[count]=value
这些事情应该会让你开始找到一个有效的解决方案。可能还有其他事情需要更改,但这些至少是您在这里犯的一些基本错误。我同意,请使用类似arraylist的内容。 已经在类中实现了,但我修复了您的代码,以防您不熟悉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;
}
}
那你想要什么?让人们为你重写吗?除非有一个特定的问题你需要帮助,考虑把这个放在TBH上,如果你要使用类似的堆栈,那就使用一个ARARYLIST。容易多了。但是,如果您想创建一个数组堆栈,那么就这样吧。然而,代码中有很多错误。我建议使用IDE来帮助您(比如Eclipse)。这不是一个发布代码并告诉人们修复代码的网站。@AlexColeman是一个更合适的选择。使用JDK 6+时不要使用
Stack
。阅读:Deque接口及其实现提供了一组更完整、更一致的LIFO堆栈操作,应该优先于此类使用。例如:Deque stack=newarraydeque()代码>-1,用于将文档引用到Java的一个古老版本(Java 1.4,hello 2002)。@Dawson由于您在这里看起来较新,请不要忘记标记对解决问题最有帮助的答案。