C# c异常索引超出范围

C# c异常索引超出范围,c#,exception,C#,Exception,我在程序中以注释的形式指出的两行出现异常。//这一行在那一行上,你能帮我清除吗。这个程序的基本思想是计算给定单词中的字符数。 苹果 a-1 p-2 l-1 e-1当您计算完所有字母后,tcount==test.length,这意味着test[tcount]将把一个元素索引到0 far 给定任何数组arr,那么arr[arr.length]将始终超出边界,因为arr是零索引的。在temp=test[tcount]之前,您需要确保tcount

我在程序中以注释的形式指出的两行出现异常。//这一行在那一行上,你能帮我清除吗。这个程序的基本思想是计算给定单词中的字符数。 苹果 a-1 p-2 l-1
e-1

当您计算完所有字母后,tcount==test.length,这意味着test[tcount]将把一个元素索引到0 far

给定任何数组arr,那么arr[arr.length]将始终超出边界,因为arr是零索引的。在temp=test[tcount]之前,您需要确保tcount 试着用“海外建筑运营管理局”这个词,它会打印o 2 o 2

如果单词中的字符顺序不必与单词中的字符顺序相同,则可以使用一个简单的实现对字符进行计数

public class Word
{
    private string _inputWord;
    public Word()
    {
        Console.WriteLine("Enter a word");
        _inputWord = Console.ReadLine();

    }

    public void sortandcount()
    {
        char[] test = _inputWord.ToCharArray();
        char temp;
        int count = 0, tcount = 0;
        Array.Sort(test);
        int length = test.Length;
        temp = test[0];

        while (length > 0)
            {
                for (int i = 0; i < test.Length; i++)
                {
                    if (temp == test[i])
                    {
                        count++;
                    }
                }
                Console.WriteLine(temp + " " + count);
                tcount = tcount + count;
                temp = test[tcount]; //this line 
                length = length - count;
                count = 0;
            }
        }



    }

    class Program
    {
        public static void Main() //this line 
        {
        Word obj = new Word();
obj.sortandcount();
        }
    }

代码包含一个bug

foreach(var pair in result.OrderBy(p=>test.IndexOf(p.Key))) {
   Console.WriteLine(pair.Key + " " + key.Value);
}
如果计数是从零开始的,那么循环将进行一次额外的迭代

 int length = test.Length; // This is not zero based
失败,因为tcount现在比测试长度大1个字符

最好的办法是

 temp = test[tcount]

请让我知道这是否有帮助:祝你有一个愉快的一天

多一点编程版本:

int length = test.Length -1;

如果要输出单词中的字母数,请尝试以下代码:

public class Word
{
    private string _inputWord;
    public Word()
    {
        Console.WriteLine("Enter a word");
        _inputWord = Console.ReadLine();
    }

    public void SortAndCount()
    {
        // sort
        char[] array = _inputWord.ToCharArray();
        Array.Sort(array);
        // for all characters
        for(int i = 0; i < array.Length; i++)
        {
            // duplicate check
            if(i > 0 && array[i] == array[i - 1])
                continue;
            // count
            int count = 0;
            for(int j = 0; j < array.Length; j++)
                if(array[i] == array[j])
                    count++;
            Console.WriteLine(array[i] + " " + count);
        }
   }
}

class Program
{
    public static void Main()
    {
        Word obj = new Word();
        obj.SortAndCount();
    }
}

这更简洁,更不容易出错。

尝试调试,单步执行代码,查看每个变量的值。这应该可以让你自己很容易地找出错误。。请注意,数组的索引为零,这意味着最后一个元素索引的长度为1。@Kjartan当然,我会尝试一下,然后在这里回复,谢谢回复谢谢你的帮助我找到了我的错误并更正了@Kjartan,@Ram很高兴能帮上忙!:我刚刚添加了这个if条件,所以还有一个迭代问题将得到解决,谢谢你帮助我if计算=test.length temp=测试[t计数];很好^^我很高兴你提出了一个解决方案。你的代码比我的更可读。但是使用两个for循环所需的迭代次数比所需的要多,所以我转到while@Ram,添加了重复检查。如果顺序应与输入相同,该怎么办
public class Word
{
    private string _inputWord;
    public Word()
    {
        Console.WriteLine("Enter a word");
        _inputWord = Console.ReadLine();
    }

    public void SortAndCount()
    {
        // sort
        char[] array = _inputWord.ToCharArray();
        Array.Sort(array);
        // for all characters
        for(int i = 0; i < array.Length; i++)
        {
            // duplicate check
            if(i > 0 && array[i] == array[i - 1])
                continue;
            // count
            int count = 0;
            for(int j = 0; j < array.Length; j++)
                if(array[i] == array[j])
                    count++;
            Console.WriteLine(array[i] + " " + count);
        }
   }
}

class Program
{
    public static void Main()
    {
        Word obj = new Word();
        obj.SortAndCount();
    }
}
var testString = "APPLE";

testString.ToCharArray()
.OrderBy(i => i).ToLookup(i => i)
.Select(i => new { letter = i.Key, count = i.Count() }).ToList()
.ForEach(i => Console.WriteLine("letter {0}, count {1}", i.letter, i.count));