java 11:数组作为函数参数,数组做为函数返回值
1 數(shù)組作為參數(shù)
我們可以將數(shù)組作為參數(shù),傳入到函數(shù)中,其實(shí)就像我們main函數(shù)中 public void main(String [] args){};就是用數(shù)組作為函數(shù)參數(shù);
又如,
[java] view plaincopy這里的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圖形表示:
這里同時(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
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
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ì)到的)
總結(jié)
以上是生活随笔為你收集整理的java 11:数组作为函数参数,数组做为函数返回值的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 详解SQL中drop、delete和tr
- 下一篇: java finalize方法的使用