Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 创建对象堆栈类-哪里出错了?_Java_Arrays_Object - Fatal编程技术网

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

我还是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;
    }
} 
  • stack
    似乎是元素类型的数组,它告诉我是
    int
  • 什么类型的通用堆栈仅限于
    int
  • setCount
    似乎是在
    GeneralStack(int)
    构造函数中设置为
    maxCapacity
  • 您的
    isEmpty()
    方法将无法正常工作,因为在
    if
    条件后有一个意外的分号<代码>如果(计数==0)表示“如果
    count
    等于零,则什么也不做”。您可能打算:

    if (count == 0) {
      isEmpty = true;
    }
    
    事实上,整个方法可以缩短为一条语句

  • #3中的相同问题也适用于
    isFull()
    ,具有类似的缩写形式。您的作用域中也没有
    maxCapacity
    变量。。。也许您忘记声明和初始化字段了
  • 分配给堆栈应该是改变对应于顶部的数组元素
  • pop()
    如果声明为
    void
    ,则不应返回任何内容。此外,
    count=count-1
    可以使用减量运算符
    --
    缩短,例如
    --count
  • 您可能想命名的是
    top()
    (根据
    pop()
    的代码判断),您无意中命名了
    topVal()
    。此外,您从不在方法范围中声明
    topVal
    变量。通过直接从数组返回元素,您可以重写该方法以完全消除对变量的需要
  • 我哪里出错了

  • 在我看来,这里似乎有一些编译错误。e、 g.您调用一个似乎不存在的函数
    top()
    (我猜您的意思是
    topVal()
  • 如果您想让堆栈接受任何类型,您应该查看
  • 在堆栈中,您将希望接受与
    push
    返回的
    pop
    参数相同的类型。您的
    push
    函数接受整数,但您的
    pop
    函数无效
  • 支持数据结构的阵列需要与上面步骤(2)中使用的阵列类型相同。现在看来,您可能希望它是一个整数,但正如步骤(1)所建议的,可能需要考虑使用泛型类型
  • 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由于您在这里看起来较新,请不要忘记标记对解决问题最有帮助的答案。