日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

java 11:数组作为函数参数,数组做为函数返回值

發(fā)布時(shí)間:2023/12/2 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 11:数组作为函数参数,数组做为函数返回值 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1 數(shù)組作為參數(shù)

我們可以將數(shù)組作為參數(shù),傳入到函數(shù)中,其實(shí)就像我們main函數(shù)中 public void main(String [] args){};就是用數(shù)組作為函數(shù)參數(shù);

又如,

[java] view plaincopy
  • public?class?ArrayPar??
  • {??
  • ??? public?static?void?printArray(int?[]?array)??
  • ??? {??
  • ??????? for(int?i=0;i<array.length;i++)?
  • ??????????? System.out.print(array[i]+"??");??
  • ??? }??
  • }??
  • 我們可以這樣調(diào)用 ArrayPar.printt(new int[ ]{3,2, 5,67});調(diào)用數(shù)組

    這里的new int[ ]{3,2,5,67};他也是一種創(chuàng)建數(shù)組的方法,只是這種方法創(chuàng)建出來(lái)的數(shù)組是沒(méi)有名字的,所以叫匿名數(shù)組。很多時(shí)候在只使用一次的時(shí)候可以使用匿名數(shù)組的方法法,類(lèi)似的還有匿名類(lèi)。

    Java uses pass-by-value to pass arguments to a method. There are important differences
    between passing the values of variables of primitive data types and passing arrays.
    ■ For an argument of a primitive type, the argument’s value is passed.
    ■ For an argument of an array type, the value of the argument is a reference to an array;
    this reference value is passed to the method. Semantically, it can be best described as
    pass-by-sharing, i.e., the array in the method is the same as the array being passed.
    So if you change the array in the method, you will see the change outside the
    method.

    java 使用值傳參(pass_by_value)的方式來(lái)傳遞函數(shù)參數(shù),只是值傳遞方式在處理原始數(shù)據(jù)類(lèi)型參數(shù)與引用類(lèi)型參數(shù)時(shí)候有不同,如果一個(gè)參數(shù)是原始數(shù)據(jù)類(lèi)型,那么參數(shù)變量的值傳遞進(jìn)去。如果是引用類(lèi)型,是傳進(jìn)了引用變量的值(也就是說(shuō),只是將指向數(shù)據(jù)的引用的值給傳進(jìn)去了,也就是被調(diào)用的函數(shù)新建的空間放的是這個(gè)引用的值,那么也就是也指向了數(shù)組存在的內(nèi)存),所以同樣是值傳遞,引用類(lèi)型的傳入的當(dāng)然是引用變量的值,指向了同一數(shù)組,那么函數(shù)內(nèi)對(duì)數(shù)組進(jìn)行的修改在函數(shù)退出后依舊是有效的。

    例子:

    [java] view plaincopy
  • public?class?ArrayPar??
  • {??
  • ????public?static?void?main(String?[]?args)??
  • ????{??
  • ????????int?x=1;??
  • ????????int?y[]={1,2,3};??
  • ????????change(x,y);??
  • ????????System.out.println("x="+x+",y[0]="+y[0]);??
  • ????}??
  • ????public?static?void?change(int?num,int?[]?array)??
  • ????{??
  • ????????num=100;??
  • ????????array[0]=100;??
  • ????}??
  • }??
  • 圖形表示:


    這里同時(shí)注意一下,當(dāng)我們用new 以及malloc這些的內(nèi)存空間是在堆上heap,而像我們被調(diào)用的函數(shù)中使用的這些變量等在棧上。在調(diào)用changes時(shí)候,x的值被傳入,在被調(diào)用的函數(shù)中重新開(kāi)辟一個(gè)空間來(lái)放這個(gè)基本數(shù)據(jù)類(lèi)型參數(shù),但是int [ ] y ,將y傳入其實(shí)就是傳入了引用,在被調(diào)用的函數(shù)的棧上只會(huì)開(kāi)辟一個(gè)空間來(lái)存放這個(gè)引用,所以被調(diào)用的函數(shù)與調(diào)用者 中兩個(gè)引用指向堆上同一塊內(nèi)存。


    2 數(shù)組做為函數(shù)返回值

    [java] view plaincopy
  • public?static<span?style="color:#ff0000;">?int?[]</span>?reverse(int?[]?array)??
  • {??
  • ????int?[]?result=new?int[array.length]??
  • ????for(int?i=0;i<array.length;i++)??
  • ????{??
  • ????????result[i]=array[lenght-i];??
  • ????}??
  • ??? return?result;
  • }??
  • 在將數(shù)組作為函數(shù)返回值時(shí)候如上紅色標(biāo)出的,就是在函數(shù)名字前加上返回值類(lèi)型是int [ ] 表示返回一個(gè)int型數(shù)組,在函數(shù)體內(nèi)最后返回是result這樣的函數(shù)引用。


    Case Study: Counting the Occurrences of Each Letter

    write?a program to count the occurrences of each letter in an random array of ?lower ?characters.

    那么我們可以怎么做呢?

    1)首先是要產(chǎn)生一個(gè)隨機(jī)char數(shù)組 ?creatArray();(是否記得前邊說(shuō)過(guò)產(chǎn)生[a,a+b)之間的一個(gè)隨機(jī)數(shù) 為 ?a+Math.random()*b,是否記得我們創(chuàng)建過(guò)獲取任意字符的一個(gè)類(lèi)?)

    2) 創(chuàng)建一個(gè)數(shù)組 int [ ] count,長(zhǎng)度26,準(zhǔn)備來(lái)存放各個(gè)字母的計(jì)數(shù)

    3)對(duì)數(shù)組進(jìn)行循環(huán) , 每讀取一個(gè)字母ch,則 count[ch-'a']++;

    [java] view plaincopy
  • class?RandomChar??
  • {??
  • ????public?static?char?getRandomChar(char?ch1,char?ch2)??
  • ????{??
  • ????????return?(char)(ch1+Math.random()*(ch2-ch1+1));??
  • ????}??
  • ????public?static?char?getLowerCaseLetter()??
  • ????{??
  • ????????return?getRandomChar('a','z');??
  • ????}??
  • ????public?static?char?getUpperCaseLetter()??
  • ????{??
  • ????????return?getRandomChar('A','Z');??
  • ????}??
  • ????public?static?char?getDigitalLetter()??
  • ????{??
  • ????????return?getRandomChar('0','9');??
  • ????}??
  • ????public?static?char?getRandomLetter()??
  • ????{??
  • ????????return?getRandomChar('\u0000','\uFFFF');??
  • ????}??
  • }??
  • public?class?CountOccur??
  • {??
  • ????public?static?void?main(String?[]?args)??
  • ????{??
  • ?????????char?[]?array=CreateArray();??
  • ?????????int?[]?count?=?new?int[26];??
  • ?????????for(int?i=0;i<array.length;i++)??
  • ?????????{??
  • ?????????????count[array[i]-'a']++;??
  • ?????????}??
  • ?????????for(int?i=0;i<count.length;i++)??
  • ?????????{??
  • ????????????System.out.print((char)(i+'a')+":?"+count[i]+"????");??
  • ????????????if((i+1)%10==0)?System.out.println();??
  • ?????????}??
  • ???????????
  • ???????????
  • ????}??
  • ????//產(chǎn)生一個(gè)100個(gè)元素的小寫(xiě)隨機(jī)數(shù)組??
  • ????public?static?char[]?CreateArray()??
  • ????{??
  • ????????char?[]?a=new?char[100];??
  • ????????for(int?i=0;i<a.length;i++)??
  • ????????{??
  • ????????????a[i]=RandomChar.getLowerCaseLetter();??
  • ????????}??
  • ????????return?a;??
  • ????}??
  • }??



  • 3 可變長(zhǎng)度參數(shù)列表

    You can pass a variable number of arguments of the same type to a method. The parameter in
    the method is declared as follows:
    typeName... parameterName
    In the method declaration, you specify the type followed by an ellipsis Only one vari-
    able-length parameter may be specified in a method, and this parameter must be the last para-
    meter. Any regular parameters must precede it.
    Java treats a variable-length parameter as an array. You can pass an array or a variable
    number of arguments to a variable-length parameter. When invoking a method with a variable
    number of arguments,
    Java creates an array and passes the arguments to it

    我們可以傳遞類(lèi)型相同,但個(gè)數(shù)可以變化的參數(shù)到函數(shù)中,如果有這個(gè)需求的話,這時(shí)候我們只需要在形式參數(shù)中使用 typename...parameterName就可以達(dá)到這個(gè)目的,要注意,在這里聲明的該變長(zhǎng)參數(shù)必須是最后一個(gè)參數(shù),任何常規(guī)參數(shù)必須在他之前,也就是說(shuō)你可以有 MethodName(char b, double c, int ... nums) 這樣的形式即int ... nums必須在最后一個(gè)位置,你不能將int ... nums 聲明在參數(shù)參數(shù)列表的非最后位置。

    java將可變長(zhǎng)參數(shù)當(dāng)做數(shù)組對(duì)待。可以將一個(gè)數(shù)組或者可的參數(shù)個(gè)數(shù)傳遞給該參數(shù)(注意,我們這里說(shuō)該參數(shù)就是 typeName ... parameterName這整個(gè)結(jié)構(gòu)),無(wú)論哪種形式,java會(huì)創(chuàng)建一個(gè)數(shù)組并把參數(shù)傳給他,注意這里體會(huì)原文說(shuō)的When invoking a method with a variable
    number of arguments, java Create an array and passes the arguments to it
    ,也就是說(shuō),如果你是傳入幾個(gè)變長(zhǎng)的變量,那么在調(diào)用時(shí)候java先將創(chuàng)建一個(gè)數(shù)組來(lái)裝這幾個(gè)實(shí)際參數(shù),然后再執(zhí)行調(diào)用的函數(shù),如果本身我們傳入一個(gè)數(shù)組,其實(shí)他并不會(huì)創(chuàng)建一個(gè)新的數(shù)組來(lái)裝,還是一樣像上面指向了已經(jīng)分配的數(shù)組空間,所以在被調(diào)用的函數(shù)中對(duì)數(shù)組的改變?cè)谕顺鰰r(shí)候還是有效的(這是我用例子試了下體會(huì)到的)

    [java] view plaincopy
  • public?class?VariablePar??
  • {??
  • ????public?static?void?main(String?[]?args)??
  • ????{??
  • ????????int?array[]={1,4,7,2,0};??
  • ????????System.out.println("max="+findMax(array));??
  • ????????ifChange(array);??
  • ????????System.out.println("array[0]="+array[0]);??
  • ????????ifChange(1,45,33);??
  • ????}??
  • ????public?static?int?findMax(int?...?nums)??
  • ????{??
  • ????????if(nums.length==0)???
  • ????????{??
  • ????????????System.out.println("No?argumment?passed");??
  • ????????????return?-1;??
  • ????????}??
  • ????????int?max=nums[0];??
  • ????????for(int?i=1;i<nums.length;i++)??
  • ????????{??
  • ????????????if(max<nums[i])?max=nums[i];??
  • ????????}??
  • ????????return?max;??
  • ????}??
  • ????//測(cè)試這里究竟是新創(chuàng)建一個(gè)數(shù)組還是相當(dāng)于把引用傳進(jìn)來(lái)而已??
  • ????public?static?void?ifChange(int?...?nums)??
  • ????{??
  • ????????nums[0]=100;??
  • ????}??
  • }??
  • 這里,我們從findMax中看到,他不用先說(shuō)明 什么就可以直接使用nums.lenght 說(shuō)明確實(shí)java是完全將這類(lèi)型的參數(shù)當(dāng)做一個(gè)數(shù)組來(lái)處理了,二在ifChange中,我們看到輸出的是array[0]=100,說(shuō)明我們?cè)谡{(diào)用ifChange(array)時(shí)候并不是重新創(chuàng)建一個(gè)新的數(shù)組,而是還是一樣像前邊的傳入了引用,被調(diào)用者還是指向了相同的數(shù)組空間。但是在ifChage(1,45,33)這個(gè)調(diào)用時(shí)候,java就會(huì)先new int[ ]{1,45,33} 這樣,然后形參nums再指向它,其實(shí)這樣返回后應(yīng)該就不會(huì)改變了1的值吧?

    總結(jié)

    以上是生活随笔為你收集整理的java 11:数组作为函数参数,数组做为函数返回值的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。