日期:2014-05-18  浏览次数:20831 次

这个属性怎么一直执行?
C# code

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

namespace MyArrayList
{
    class SArrayList
    {
        private string[] str;

        public SArrayList()
        {
            str = null;
        }

        public SArrayList this[int index]
        {
            get   // [color=#FF0000][u][b]这个地方在输出的时候一直不停运行,怎么回事[/b][/u][/color]
            {
                return this[index];
            }
            set
            {
                this[index] = (SArrayList)value;
            }
        }

        public int Length
        {
            get
            {
                if (str == null)
                    return 0;
                else
                    return str.Length;
            }
        }

        public void Add(string s)
        {            
                string[] temp = new string[Length + 1];
                for (int i = 0; i < Length; ++i)
                    temp[i] = str[i];
                temp[Length] = s;
                str = temp;
           
        }

        public int Find(string s)
        {
            if (Length == 0)
            {
                Console.Write("数组是空的!");
                return -1;
            }
            for (int i = 0; i < Length; ++i)
                if (str[i] == s)
                    return i;
            return -1;
        }

        public void Remove(string s)
        {
            if (Find(s) == -1)
                return;
            int index = Find(s);
            string[] temp = new string[Length - 1];
            for (int i = 0; i < Length; i++)
            {
                if (i == index)
                    continue;
                else if (i > index)
                    temp[i - 1] = str[i];
                else
                    temp[i] = str[i];                
            }
            str = temp;
        }

        public void Clear()
        {
            for (int i = 0; i < Length; ++i)
                Remove(str[i]);
        }
    }
}

测试代码:
namespace MyArrayList
{
    class Program
    {
        static void Main(string[] args)
        {           
            SArrayList sal = new SArrayList();
            for (int i = 0; i < 5; ++i)
            {
                sal.Add(i.ToString());               
            }
            for (int i = 0; i < 5; ++i)
                Console.Write(sal[i]+"\t");
        }
    }
}



------解决方案--------------------
调用自身了。
------解决方案--------------------
循环调用
------解决方案--------------------
我靠....

堆栈移出, 死循环了


public SArrayList this[int index]
{
get // [b]这个地方在输出的时候一直不停运行,怎么回事[/b]
{
return this[index];
}
set
{
this[index] = (SArrayList)value;
}
}

------解决方案--------------------
索引器的问题,应该是:
 public string this[int index]
{
get // [b]这个地方在输出的时候一直不停运行,怎么回事[/b]
{
return str[index];
}
set
{
str[index] = value;
}
}