我怎样才能在这里修复错误。 [英] How can I fixe an error here.

查看:62
本文介绍了我怎样才能在这里修复错误。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

static void Main(string[] args)
{
    byte[] barr = GenerateRandomArrForByte(1000);
    WriteArr(barr);

    byte[] countArr = SortByteArr(barr);
    WriteArr(countArr);

    Console.ReadLine();
}

static byte[] GenerateRandomArrForByte(int n)
{
    byte[] arr = new byte[n];
    Random rnd = new Random();
    for (int i = 0; i < n; i++)
    {
        arr[i] = (byte)rnd.Next(0, 256);
    }
    return arr;
}

static void WriteArr(byte[] arr)
{
    for (int i = 0; i < arr.Length; i++)
    {
        Console.WriteLine("arr[{0}] = {1}",i,arr[i]);
    }
}

static byte[] SortByteArr(byte[] arr)
{
    int[] countArr = new int[256];
    foreach (byte a in arr)
    {
        countArr[a]++;
    }

    int index = 0;
    byte[] arr1 = new byte[arr.Length];
    for (byte i = 0; i < countArr.Length; i++)
    {
        int count = countArr[i];
        for (int j = 0; j < count; j++)
        {
            arr1[index] = i;
            index++;
        }
    }
    return arr1;
}





我尝试过:



它带来了这个错误索引超出了数组的范围。

这里

arr1 [index] = i;



What I have tried:

It bring this error "Index was outside the bounds of the array."
here
arr1[index] = i;

推荐答案

让我们先看一下错误及其含义:

Let's start by looking at the error and what it means:
Index was outside the bounds of the array



这意味着您已尝试使用数字索引来访问不存在的数组元素 - index值为负数,大于数组可以拥有的最大索引 - 这是数组中元素的数量减去1,因为C#数组索引总是从零开始。

如果你的桌子是一个抽屉数组,它有三个抽奖索引将是0,1和2 - 尝试访问抽屉[3]或抽屉[4],它不存在,所以你得到一个错误。



现在看看你的代码:


That means that you have tried to use a numeric index to access an array element that doesn't exist - the "index" value is negative, of greater than the largest index the array can have - which is the number of elements in the array minus one, because C# array indexes always start at zero.
If your desk was an array of drawers and it had three draws the indexes would be 0, 1, and 2 - try to access drawers[3] or drawers[4] and it doesn't exist so you get an error.

Now look at your code:

static byte[] SortByteArr(byte[] arr)
{
    int[] countArr = new int[256];
    foreach (byte a in arr)
    {
        countArr[a]++;
    }

    int index = 0;
    byte[] arr1 = new byte[arr.Length];
    for (byte i = 0; i < countArr.Length; i++)
    {
        int count = countArr[i];
        for (int j = 0; j < count; j++)
        {
            arr1[index] = i;
            index++;
        }
    }
    return arr1;
}



你的外环循环,而 i 小于你的计数数组的大小 - 这是256次:我将从0到255(包括0和255)。在其中,你有一个第二个循环,它将循环与每个元素中的值一样多次:所以如果所有元素都是一个,你将在内循环中执行代码256 * 1次,它们都是2,它将执行256 * 2次== 512.

在内部循环中,你将索引用于aar1并增加它 - 所以你需要在arr1中有足够的元素来绕过它可能是巨大的次数!并且因为你的arr1将是1000个元素,并且每个元素得到0到25​​5之间的数字,你需要arr平均有256 * 128个元素(外部循环的256倍乘以数组中的平均值)。 br />


如果该代码应该按照名称的意思对数组进行排序,那就不会做得很好......

我' d建议您学习使用调试器,并制定一个更好的排序算法!


Your outer loop goes round while i is less than the size of your count array - which is 256 times: i will be from 0 to 255 inclusive. Inside that, you have a second loop which will go round as many times as the value in each element: so if all the elements were one, you would execute the code inside the inner loop 256 * 1 times, and they were all 2, it would execute 256 * 2 times == 512.
And inside that inner loop, you use the index into aar1 and increment it - so you need to have sufficient elements in arr1 to go round potentially a huge number of times! And since your arr1 is going to be 1000 elements, and each element gets a number between 0 and 255 you need arr to have an average of 256 * 128 elements (256 times round the outer loop times the average value in the array).

If that code is supposed to sort an array as the name suggests, it doesn't do a good job...
I'd suggest that you learn to use the debugger, and work out a rather better sort algorithm!


Quote:

它带来了这个错误索引超出了数组的范围。

这里

It bring this error "Index was outside the bounds of the array."
here

arr1[index] = i; 



消息非常明显: index 用于在结束后访问元素数组。



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到它有一个停止做你期望的点。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - A初学者指南 [ ^ ]



调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。 />
当代码不做ex的时候你已经接近了一个错误。


The message is pretty obvious: index is used to access an element after the end of the array.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.


这篇关于我怎样才能在这里修复错误。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆