将数组传递给特定类型的函数

时间:2018-08-14 15:21:10

标签: c#

我需要有关此代码的帮助。我无法使其正常工作..对我来说尚不清楚。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Solution
{
    static int[] foo(int[] array)
    {
        int[] xx = new int[array.Length];

        for(int i = 0; i < array.Length; i++)
        {
            array[i] *= 2;
            Console.WriteLine(array[i]);
        }

        return array;
    }

    static void Main(string[] args)
    {

        int[] array = new int[4] {73, 67, 38, 33 };

        foo(array);

    }
}

如何重建 foo 函数以从 array 返回值?

我看到了很多带有void函数的帮助链接..它们没有用。当涉及到特定类型的功能时,我无法使其正常工作。

谢谢

ddr8

2 个答案:

答案 0 :(得分:1)

    class Solution
    {
        static int[] foo(int[] array)
        {
            int[] xx = new int[array.Length]; // Building this but not using it

            for (int i = 0; i < array.Length; i++)
            {
                array[i] *= 2; //Altering the input array changes the array in the Main method..

                Console.WriteLine(array[i]);
            }

            return array;
        }

        static void Main(string[] args)
        {


            int[] array = new int[4] { 73, 67, 38, 33 };

            foo(array); // Not assigning the values to an object.
            //After this step look at array it will be 146, 134, 76 , 66
        }
    }

因此,您正在foo方法中更改原始数组。您要传递数组对象,然后覆盖值。

您声明了一个新的int [] xx,但是对此却什么也不做,我认为您应该将源数组复制到新的xx int []。 这样做不会改变main方法中的原始int。 然后,您可以在main方法中分配新数组的返回值。 下面的示例:

    class Solution
    {
        static int[] foo(int[] array)
        {

        int[] xx = new int[array.Length];
        //Copy the array into the new array
        Array.Copy(array, xx, array.Length);  

            for (int i = 0; i < xx.Length; i++)
            {
                xx[i] *= 2; 

                Console.WriteLine(xx[i]);
            }
            return xx;
        }

        static void Main(string[] args)
        {
            int[] array = new int[4] { 73, 67, 38, 33 };
            //Assign the new array to an object
            int[] newArray = foo(array);
        }
    }

编辑: 我还看到您在顶部包括了linq,如果您对使用linq感兴趣,那么将获得相同的结果:

            static void Main(string[] args)
            {
                        int[] array = new int[4] { 73, 67, 38, 33 };
                        int[] newarr = array.Select(arrayvalue => arrayvalue * 2).ToArray();
            }

答案 1 :(得分:0)

您当前的代码确实返回一个数组,但是您是:

  1. 更新array中的值,而不是更新新的xx数组中的值
  2. 未在Main中分配返回值

在这里您要修改现有阵列而不是新阵列:

static int[] foo(int[] array)
{
    int[] xx = new int[array.Length];

    for(int i = 0; i < array.Length; i++)
    {
        // NOTE: modifying array, not xx
        array[i] *= 2;
        Console.WriteLine(array[i]);
    }

    // NOTE: returning array, not xx -- xx is not used
    return array;
}

这是返回的数组缺少的分配:

static void Main(string[] args)
{
    int[] array = new int[4] {73, 67, 38, 33 };
    // You are not assigning the returned array here
    int[] newArray = foo(array);
}

如果需要更改数组大小,另一个选择是将数组作为ref参数传递:

static int[] foo(ref int[] array)
{
    // Modify array as necessary
}