(一)數組的創建
數組的創建包括兩部分:數組的申明與分配內存空間。
1
2
|
int score[]= null ; //申明一維數組 score= new int [ 3 ]; //分配長度為3的空間 |
數組的申明還有另外一種方式:
int[] score=null; //把中括號寫在數組名前面
通常,在寫代碼時,為了方便,我們將兩行合并為一行:
int score[]=new int score[3]; //將數組申明與分配內存寫在一行
(二)傳遞參數
由于初學java,這里只討論值傳遞,不考慮地址傳遞。主要有3點:
· 實參為數組名;
· 形參為新申明的數組,如果有返回值,需在函數的類型后加中括號“[]”;
· 返回值為數組名。
例子如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
/** * 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中數組的創建與傳參方法,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
原文鏈接:http://www.cnblogs.com/lijiaman/archive/2016/09/16/5876889.html