【问题标题】:Creating a constructor that pushes a number to an array length创建一个将数字推送到数组长度的构造函数
【发布时间】:2011-08-01 19:51:09
【问题描述】:

现在我正在创建一个堆栈类。主程序是:

class Program
{
    static void Main(string[] args)
    {
        Queue myQue = new Queue(5);
        Stack myStack = new Stack(5);

        myStack.Push(1);
        myStack.Push(2);
        myStack.Push(3);
        myStack.Push(4);
        myStack.Push(5);
        myStack.Push(6);

        while (!myStack.IsEmpty)
        {
            Console.WriteLine(myStack.Pop());
        }

        Console.WriteLine(myStack.Pop());

        Console.WriteLine("End of Stack");
    }
}

那么Stack Class如下:

class Stack
{

    private int top;

    private int[] anArray;

    public bool IsFull
    {
        get
        {         
            return top == anArray.Length - 1;
        }
    }

    public bool IsEmpty
    {
        get
        {
            return top == -1;
        }
    }

    public void Push(int valueToPush)
    {

        if (IsFull)
        {
            //do nothing
        }
        else
        {
            anArray[top] = valueToPush;
            top = top + 1;
        }
    }
    public int Pop()
    {
        if (IsEmpty)
        {
            //do nothing
            return 
        }
        else
        {
            int pop = anArray[top];
            top = top -1;
            return pop;
        }
    }
}

我遇到的问题是,如果它为空,我不需要返回任何内容,但由于 int 类型,它不会让我返回 NULL。

然后我想我要么跳过/不明白什么是“构造函数”。我明白当我实例化“Stack myStack = new Stack(5);”时它正在发送堆栈类“5”,但我如何将堆栈类中的 5 放入数组中?

【问题讨论】:

  • 提供的 Stack 类没有采用整数的构造函数。你确定那是你用的吗? System.Collections.Stack 有一个带有单个整数的,它设置堆栈的初始大小,它不会向其中压入任何东西。
  • 这听起来很像课堂作业?
  • 我正在创建一个堆栈类,而不是使用 C# 本身定义的堆栈类。我想我需要在 Stack 类中创建一个构造函数来获取 5,然后将其设为数组长度。
  • 为什么不在 Stack 中包含一个构造函数,例如 Stack(int i) ?此外,如果您需要在 Pop 上返回 null,您可以使用 nullable int。我个人会抛出异常
  • 异常时+1,弹出空堆栈是一个逻辑错误,不应该是优雅的(通常不在大多数类库中)。但是,如果这是您的要求(无论是上课还是工作),那么可以为 null 的 int (System.Nullable ==or== int?) 就可以了。

标签: c# arrays constructor stack


【解决方案1】:

你没有构造函数。

在你的堆栈类中添加这样的东西:

public Stack(int num)
{
    Push(num);
}

只需阅读您希望将数字用于创建数组大小的评论,这样您就可以这样做:

int arrayLength;
public Stack(int num)
{
    arrayLength = num;
    //doSomething() -> call a method or just create the array
}

【讨论】:

    【解决方案2】:

    您必须返回null 的一个选项是将返回类型更改为int?但是您将使用 nullable type 而不是直接使用 int。

    public int? Pop()
        {
            if (IsEmpty)
            {
                //do nothing
                return null;
            }
    ...
    

    就构造函数而言,这就是您设置类的方式。是 5 假设确定您的堆栈的大小还是应该是第一个添加到堆栈中的东西?

    例如,如果构造函数旨在设置堆栈的大小,您将执行以下操作。

    class Stack
    {
    
        private int top;
    
        private int[] anArray;
    
        //This is your constructor. It will guarantee that your anArray will be initialized
        public Stack(int size)
        {
            anArray = new int[size];
        }
    
        ...
    

    【讨论】:

    • 我想你的意思是 anArray = new int[size];
    • 嗯,5 是 arraylength 需要的。而 push.(1) 是数组中的第一个数字。
    • 是的,先生,它工作得很好。现在唯一的问题是退货。我认为将其更改为可空类型会使我的程序崩溃。
    【解决方案3】:

    在大多数情况下,当您创建堆栈(new Stack(5))时,您会传递值 5,该值用于确定堆栈的 SIZE(请参阅http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=65)。

    在您当前的堆栈实现中,您没有指定构造函数。您需要按照以下方式创建一些东西:

    public Stack(int x) {
       // initialize your array (anArray) that represents a stack to size 5
    }
    

    【讨论】:

      【解决方案4】:

      1) 尝试Pop 空堆栈中的一个项目可以被视为无效操作,所以如果你只允许用户检查堆栈是否为空(我看到你这样做了),这是完全正确的到throw new InvalidOperationException("The stack is empty.")那里。

      2) 构造函数问题 - 您的代码中没有构造函数。构造函数看起来像一个方法,但它没有返回值并且与您的类同名。它由new 运算符调用,并且可以像每个方法一样接受参数。所以你可以像这样接受5

      public Stack(int depth)
      {
          // do something with depth
      }
      

      【讨论】:

        猜你喜欢
        • 2021-09-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-04
        • 1970-01-01
        相关资源
        最近更新 更多