简体   繁体   English

系统索引超出范围C#

[英]system index out of range c#

I'm trying to make a program which will figure out which half of an array added together is greater, however keep getting this error and I can not seem to figure out why 我正在尝试制作一个程序,该程序将找出加在一起的数组的哪一半更大,但是不断出现此错误,我似乎无法弄清楚为什么

"An unhandled exception of type 'System.IndexOutOfRangeException' occurred in Average Mark.exe" on line 31 when trying to work from the back of the array. 尝试从数组背面工作时,在第31行上,“平均值Mark.exe中发生了'System.IndexOutOfRangeException类型的未处理异常”。

I am very new to c# and thought what I was doing should have worked? 我对C#非常陌生,以为我在做什么应该有效? Many thanks! 非常感谢!

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

namespace most_frequent_int
{
    class Program
    {
        static void Main(string[] args)
        {
            halfCheck(new int[] {1,1,2,3});
        }

        static void halfCheck(int[] checkArray)
        {
            int fHalf = 0; 
            int sHalf=0;

            //even method
            if (checkArray.Length % 2 == 0) 
            {               
                for (int i= 0;i<checkArray.Length/2;i++)//check first half even
                {
                    fHalf = fHalf + checkArray[i];
                }

                for (int i=checkArray.Length;i>checkArray.Length/2;i--)
                {
                    sHalf = sHalf + checkArray[i];
                    Console.WriteLine(sHalf);
                }

                if (fHalf > sHalf)
                {
                    Console.WriteLine("The first half is bigger");
                }
                else
                {
                    Console.WriteLine("The second half is bigger");
                }
                Console.ReadLine();

            }

            //odd method
            else
            {
                Console.WriteLine("odd");
            }   
        }
    }
}

The error is in this code: 错误在以下代码中:

for (int i=checkArray.Length;i>checkArray.Length/2;i--)
{
    sHalf = sHalf + checkArray[i];
    Console.WriteLine(sHalf);
}

The Length property is 1-based, as it counts the elements in the array, but indexers are zero-based. Length属性基于1,因为它计算数组中的元素,但索引器基于0。 When you try to access checkArray[i] , the i value is past the end of the array. 当您尝试访问checkArray[i]i值超出了数组的末尾。 Consider starting with checkArray.Length - 1 . 考虑从checkArray.Length - 1

On this line: 在这行上:

for (int i = checkArray.Length; i > checkArray.Length / 2; i--)

You are assigning i to the array's length on the first iteration. 您将在第一次迭代中将i分配给数组的长度。 You cannot index an array by its length, at it is one more than the last valid position. 您不能按数组的长度对其进行索引,因为数组的长度比最后一个有效位置大一。

Try this: 尝试这个:

for (int i = checkArray.Length - 1; i > checkArray.Length / 2; i--)

The problem is in the following line: 问题在以下行中:

 for (int i=checkArray.Length;i>checkArray.Length/2;i--)

The first element you access is checkArray[checkArray.Length] which is out of the array. 您访问的第一个元素是checkArray[checkArray.Length] ,它不在数组中。 The allowed indexes are to checkArray.Length -1 . 允许的索引是checkArray.Length -1

Try with: 尝试:

for (int i=checkArray.Length - 1; i > checkArray.Length/2; i-- )

This line: 这行:

for (int i=checkArray.Length;i>checkArray.Length/2;i--)

Will throw an IndexOutofRangeException every time. 每次都会抛出IndexOutofRangeException。 When using arrays, the .length property returns the number of elements in the array, which is NOT the same as the highest index. 使用数组时, .length属性返回数组中元素的数量,该数量与最高索引不同。

For example, if checkArray has 5 elements, the indexes will be 0, 1, 2, 3, and 4. checkArray.Length will return the number of elements, which is 5. 例如,如果checkArray有5个元素,则索引将为0、1、2、3和checkArray.Length将返回元素数,即5。

Since your for loop begins with i=checkArray.length , the first index it tries will ALWAYS be out of bounds. 由于您的for循环以i=checkArray.length ,因此它尝试的第一个索引将始终超出范围。 Try i=checkArray.Length-1 instead. 尝试改用i=checkArray.Length-1

You just need to change what i starts in the second loop. 您只需要更改我在第二个循环中开始的内容即可。 i = checkArray.Length - 1 我= checkArray.Length-1

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

namespace most_frequent_int
{
class Program
{
    static void Main(string[] args)
    {
     halfCheck(new int[] {1,1,2,3});
    }

    static void halfCheck(int[] checkArray)
    {
        int fHalf = 0; 
        int sHalf=0;

        //even method
        if (checkArray.Length % 2 == 0) 
        {               
            for (int i= 0;i<checkArray.Length/2;i++)//check first half even
            {
                fHalf = fHalf + checkArray[i];
            }

        for (int i=checkArray.Length - 1;i>checkArray.Length/2;i--)
            {
                sHalf = sHalf + checkArray[i];
                Console.WriteLine(sHalf);
            }

            if (fHalf > sHalf)
            {
                Console.WriteLine("The first half is bigger");
            }
            else
            {
                Console.WriteLine("The second half is bigger");
            }
            Console.ReadLine();

        }

        //odd method
        else
        {
            Console.WriteLine("odd");
        }

    }

}
}

The array is indexed starting at 0 but length starts at 1. So your array length is 4 and but checkArray[4] doesn't exist as the last item in the array is 3. Length -1 should fix it. 数组从0开始索引,但长度从1开始。因此,数组长度为4,但由于数组的最后一项为3,所以checkArray [4]不存在。长度-1应该可以解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM