(一)數(shù)組的創(chuàng)建
數(shù)組的創(chuàng)建包括兩部分:數(shù)組的申明與分配內(nèi)存空間。
int score[]=null; //申明一維數(shù)組score=new int[3]; //分配長度為3的空間
數(shù)組的申明還有另外一種方式:
int[] score=null; //把中括號寫在數(shù)組名前面
通常,在寫代碼時,為了方便,我們將兩行合并為一行:
int score[]=new int score[3]; //將數(shù)組申明與分配內(nèi)存寫在一行
(二)傳遞參數(shù)
由于初學(xué)java,這里只討論值傳遞,不考慮地址傳遞。主要有3點(diǎn):
? 實(shí)參為數(shù)組名;
? 形參為新申明的數(shù)組,如果有返回值,需在函數(shù)的類型后加中括號“[]”;
? 返回值為數(shù)組名。
例子如下:
/*** Created by lijiaman on 2016/9/16.*/public class createArray2{public static void main(String[] args){int score[]=null;score=new int[3];int speed[]={12,35};for(int x=0;x<3;x++){score[x]=x*2+1;}for(int x=0;x<3;x++){System.out.println("score["+x+"]="+score[x]);}System.out.println("length:"+score.length);for(int x=0;x<speed.length;x++){System.out.println("Speed:"+speed);}}}以上所述是小編給大家介紹的Java中數(shù)組的創(chuàng)建與傳參方法,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!
新聞熱點(diǎn)
疑難解答
圖片精選