首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

泛型,仿照堆栈的类,报错

2013-01-04 
泛型,模仿堆栈的类,报错。using Systemusing System.Collections.Genericusing System.Linqusing System

泛型,模仿堆栈的类,报错。
泛型,仿照堆栈的类,报错

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test
{
    class MyStack<T>
    {
        public T[] Myarray;
        public int count;
        public MyStack(int m)
        {
            Myarray = new T[m];
            count = 0;
        }

        public void push(T a)
        {
            Myarray[count++] = a;

        }
        public T pull()
        {
            return Myarray[--count];
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            MyStack<int> myStack = new MyStack<int>(10);
            myStack.push(1);
            myStack.push(2);
            myStack.push(3);
            for (int i = 0; 1 < 3; i++)
            {
                Console.WriteLine(myStack.pull());
            }

          
        }
    }
}
[解决办法]

            for (int i = 0; 1 < 3; i++)
             {
                 Console.WriteLine(myStack.pull());
             }

1<3
死循环呢,下标早就越界了。再一个,你写的类型不应该有这种缺陷,需要解决。
[解决办法]
引用:
C# code?1234            for (int i = 0; 1 < 3; i++)             {                 Console.WriteLine(myStack.pull());             }


1<3
死循环呢,下标早就越界了。再一个,你写的类型不应该有这种缺陷,需要解决。



对的..问题就再这里..你这一直循环..然后count变到-1时..就会报错了..

热点排行