简体   繁体   English

如何在 C# 的数组中找到某个 position 的值?

[英]How do I find the value at a certain position in an Array in C#?

I need to iterate through an Array and find the sum of the odd, and the even positioned values.我需要遍历一个数组并找到奇数和偶数定位值的总和。

Here is what I have, but I'm getting the error: Cannot apply indexing with [] to an expression of type 'Array'这是我所拥有的,但我收到错误消息: Cannot apply indexing with [] to an expression of type 'Array'

static void SumOfOddsAndEvens(Array arr)
        {
            int i = 0;
            bool isEven = true;
            int evenSum = 0;
            int oddSum = 0;

            while (i < arr.Length)
            {
                Console.WriteLine(arr[i]);

                if (isEven) evenSum += arr[i]; // Cannot apply indexing with [] to an expression of type 'Array'
                if (!isEven) oddSum += arr[i];
                isEven = !isEven;
                i++;
            }

            Console.WriteLine("Sum of even slots: " + evenSum);

            Console.WriteLine("Sum of odd slots: " + oddSum);
        }

Function is being called here: Function 在这里被调用:

 static void Main(string[] args)
        {
            SumOfOddsAndEvens(new int[] { 1, 2, 3, 4, 6, 7, 8 });
        }

Although all arrays in C# derive from thebase-class Array , the latter does not define an indexer.尽管 C# 中的所有 arrays 都派生自基类Array ,但后者没有定义索引器。 Thus you can´t use arr[i] when arr is just an Array .因此,当arr只是一个Array时,您不能使用arr[i]

However I wonder why you even use that base-class, instead of the actual one, which is int[] in your case:但是我想知道为什么您甚至使用该基类,而不是实际的基类,在您的情况下是int[]

static void SumOfOddsAndEvens(int[] arr)
{
    int i = 0;
    bool isEven = true;
    int evenSum = 0;
    int oddSum = 0;

    while (i < arr.Length)
    {
        Console.WriteLine(arr[i]);

        if (isEven) evenSum += arr[i];
        if (!isEven) oddSum += arr[i];
        isEven = !isEven;
        i++;
    }

    Console.WriteLine("Sum of even slots: " + evenSum);

    Console.WriteLine("Sum of odd slots: " + oddSum);
}

Using the base-class would also allow the following, which is pure non-sense:使用基类还允许以下内容,这纯粹是胡说八道:

SumOfOddAndEvens(new[] { "Hello",  "World" };

or even this:甚至这个:

SumOfOddAndEvens(new object[] { 1, "World" };

You can't use an indexer on an Array.您不能在数组上使用索引器。 The Array class is a base class for all array types, and arrays are implicitly inherit from Array.数组 class 是所有数组类型的基础 class,并且 arrays 隐式继承自 Array。 But, Array itself doesn't have an indexer.但是,Array 本身没有索引器。 Here is a demonstration of your error:这是您的错误的演示:

int[] numbers = new[] {1, 2, 3, 4, 5};

numbers[2] = 11; // Okay

Array arr = numbers as Array;

arr[2] = 11; // ERROR!

You are creating your parameter right ( new int[] { 1, 2, 3, 4, 6, 7, 8 } ) but in the SumOfOddsAndEvens function you are actually using Array type.您正在创建参数权( new int[] { 1, 2, 3, 4, 6, 7, 8 } ),但在 SumOfOddsAndEvens function 中,您实际上使用的是 Array 类型。 ( static void SumOfOddsAndEvens(Array arr) ). static void SumOfOddsAndEvens(Array arr) )。 Just change this last one to int[] and you will be able to use an indexer with it.只需将最后一个更改为int[] ,您就可以使用索引器。

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

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