數(shù)組可以作為參數(shù)傳遞給方法。例如,下面的例子就是一個(gè)打印int數(shù)組中元素的方法。
public static void printArray( int [] array) {
for ( int i = 0 ; i < array.length; i++) {
System.out.print(array[i] + " " );
}
}
|
下面例子調(diào)用printArray方法打印出 3,1,2,6,4和2:
printArray( new int []{ 3 , 1 , 2 , 6 , 4 , 2 });
|
數(shù)組作為函數(shù)的返回值
public static int [] reverse( int [] list) {
int [] result = new int [list.length];
for ( int i = 0 , j = result.length - 1 ; i < list.length; i++, j--) {
result[j] = list[i];
}
return result;
}
|
以上實(shí)例中result數(shù)組作為函數(shù)的返回值。
![]() | ![]() .. 定價(jià):¥45 優(yōu)惠價(jià):¥42 更多書籍 |
![]() | ![]() .. 定價(jià):¥225 優(yōu)惠價(jià):¥213 更多書籍 |