猿问

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

我需要有关此代码的帮助。我不能让它工作,所以我还不清楚。


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函数以从数组返回值?


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


谢谢


DDR8


森林海
浏览 178回答 2
2回答

慕娘9325324

&nbsp; &nbsp; class Solution&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static int[] foo(int[] array)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] xx = new int[array.Length]; // Building this but not using it&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < array.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; array[i] *= 2; //Altering the input array changes the array in the Main method..&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(array[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return array;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] array = new int[4] { 73, 67, 38, 33 };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foo(array); // Not assigning the values to an object.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //After this step look at array it will be 146, 134, 76 , 66&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }所以你正在改变 foo 方法中的原始数组。您正在传递数组对象,然后覆盖这些值。您声明了一个新的 int[] xx 但随后什么都不做,我认为您应该将源数组复制到新的 xx int[]。这样做不会改变主方法中的原始整数。然后,您可以在 main 方法中分配新数组的返回值。下面的例子:&nbsp; &nbsp; class Solution&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static int[] foo(int[] array)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int[] xx = new int[array.Length];&nbsp; &nbsp; &nbsp; &nbsp; //Copy the array into the new array&nbsp; &nbsp; &nbsp; &nbsp; Array.Copy(array, xx, array.Length);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < xx.Length; i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xx[i] *= 2;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(xx[i]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return xx;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] array = new int[4] { 73, 67, 38, 33 };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Assign the new array to an object&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] newArray = foo(array);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }编辑:我还看到您在顶部包含了 linq,如果您对使用 linq 感兴趣,这将获得相同的结果:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] array = new int[4] { 73, 67, 38, 33 };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] newarr = array.Select(arrayvalue => arrayvalue * 2).ToArray();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

一只名叫tom的猫

您当前的代码确实返回一个数组,但您是:更新值array而不是新xx数组不分配返回值&nbsp;Main这是您修改现有数组而不是新数组的地方:static int[] foo(int[] array){&nbsp; &nbsp; int[] xx = new int[array.Length];&nbsp; &nbsp; for(int i = 0; i < array.Length; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; // NOTE: modifying array, not xx&nbsp; &nbsp; &nbsp; &nbsp; array[i] *= 2;&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(array[i]);&nbsp; &nbsp; }&nbsp; &nbsp; // NOTE: returning array, not xx -- xx is not used&nbsp; &nbsp; return array;}这是返回数组的缺失赋值:static void Main(string[] args){&nbsp; &nbsp; int[] array = new int[4] {73, 67, 38, 33 };&nbsp; &nbsp; // You are not assigning the returned array here&nbsp; &nbsp; int[] newArray = foo(array);}ref如果您需要更改数组大小,您的另一个选择是将数组作为参数传递:static int[] foo(ref int[] array){&nbsp; &nbsp; // Modify array as necessary}
随时随地看视频慕课网APP
我要回答