读书人

泛型仿照堆栈的类报错

发布时间: 2013-01-04 10:04:12 作者: rapoo

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

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时..就会报错了..

读书人网 >C#

热点推荐