第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

將數(shù)組傳遞給特定類(lèi)型的函數(shù)

將數(shù)組傳遞給特定類(lèi)型的函數(shù)

C#
森林海 2021-11-28 16:36:03
我需要有關(guān)此代碼的幫助。我不能讓它工作,所以我還不清楚。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函數(shù)以從數(shù)組返回值?我看到了很多關(guān)于 void 函數(shù)的幫助鏈接……但它們沒(méi)有用。當(dāng)涉及到特定類(lèi)型的功能時(shí),我無(wú)法使其工作。謝謝DDR8
查看完整描述

2 回答

?
慕娘9325324

TA貢獻(xiàn)1783條經(jīng)驗(yàn) 獲得超4個(gè)贊

    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 方法中的原始數(shù)組。您正在傳遞數(shù)組對(duì)象,然后覆蓋這些值。


您聲明了一個(gè)新的 int[] xx 但隨后什么都不做,我認(rèn)為您應(yīng)該將源數(shù)組復(fù)制到新的 xx int[]。這樣做不會(huì)改變主方法中的原始整數(shù)。然后,您可以在 main 方法中分配新數(shù)組的返回值。下面的例子:


    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,如果您對(duì)使用 linq 感興趣,這將獲得相同的結(jié)果:


            static void Main(string[] args)

            {

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

                        int[] newarr = array.Select(arrayvalue => arrayvalue * 2).ToArray();

            }


查看完整回答
反對(duì) 回復(fù) 2021-11-28
?
一只名叫tom的貓

TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超3個(gè)贊

您當(dāng)前的代碼確實(shí)返回一個(gè)數(shù)組,但您是:

  1. 更新值array而不是新xx數(shù)組

  2. 不分配返回值 Main

這是您修改現(xiàn)有數(shù)組而不是新數(shù)組的地方:

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;

}

這是返回?cái)?shù)組的缺失賦值:


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如果您需要更改數(shù)組大小,您的另一個(gè)選擇是將數(shù)組作為參數(shù)傳遞:


static int[] foo(ref int[] array)

{

    // Modify array as necessary

}


查看完整回答
反對(duì) 回復(fù) 2021-11-28
  • 2 回答
  • 0 關(guān)注
  • 204 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)