本章目標
- 掌握數(shù)組的引用傳遞
- 可以使用方法接收或返回一個數(shù)組
- 了解Java對數(shù)組的操作支持
1犬庇、接收和返回數(shù)組
一個方法可以接收一個數(shù)組,也可以返回一個數(shù)組谅猾,如果方法接收一個數(shù)組的話抗果,則此方法對數(shù)組所做的方法將全部被保留下來踢械。
public class ArrayRefDemo01{
public static void main(String args[]){
int temp[] = {1,3,5} ; // 利用靜態(tài)初始化方式定義數(shù)組
fun(temp) ; // 傳遞數(shù)組
for(int i=0;i<temp.length;i++){
System.out.print(temp[i] + "、") ;
}
}
public static void fun(int x[]){ // 接收整型數(shù)組的引用
x[0] = 6 ; // 修改第一個元素
}
};
輸出結(jié)果:6绽诚、3典徊、5
方法除了可以接收數(shù)組之外杭煎,也可以通過返回一個數(shù)組,只需要在返回值類型上宫峦,明確地聲明出返回的類型是數(shù)組即可岔帽。
public class ArrayRefDemo02{
public static void main(String args[]){
int temp[] = fun() ; // 通過方法實例化數(shù)組
print(temp) ; // 打印數(shù)組內(nèi)容
}
public static void print(int x[]){
for(int i=0;i<x.length;i++){
System.out.print(x[i] + "、") ;
}
}
public static int[] fun(){ // 返回一個數(shù)組
int ss[] = {1,3,5,7,9} ; // 定義一個數(shù)組
return ss ;
}
};
輸出結(jié)果:1导绷、3犀勒、5、7妥曲、9
2贾费、范例
2.1、定義排序方法
public class ArrayRefDemo03{
public static void main(String args[]){
int score[] = {67,89,87,69,90,100,75,90} ; // 定義整型數(shù)組
int age[] = {31,30,18,17,8,9,1,39} ; // 定義整型數(shù)組
sort(score) ; // 數(shù)組排序
print(score) ; // 數(shù)組打印
System.out.println("\n---------------------------") ;
sort(age) ; // 數(shù)組排序
print(age) ; // 數(shù)組打印
}
public static void sort(int temp[]){ // 執(zhí)行排序操作
for(int i=1;i<temp.length;i++){
for(int j=0;j<temp.length;j++){
if(temp[i]<temp[j]){
int x = temp[i] ;
temp[i] = temp[j] ;
temp[j] = x ;
}
}
}
}
public static void print(int temp[]){ // 輸出數(shù)組內(nèi)容
for(int i=0;i<temp.length;i++){
System.out.print(temp[i] + "\t") ;
}
}
};
輸出結(jié)果:
67 69 75 87 89 90 90 100
---------------------------
1 8 9 17 18 30 31 39
以上只是完成了整型數(shù)組的排序操作檐盟,如果一個操作中及要求可以排序整型也可以排序浮點型等各種數(shù)據(jù)褂萧,如果分別實現(xiàn)則會比較麻煩,所以在Java中為了操作數(shù)組方便葵萎,提供了一個支持导犹。
public class ArrayRefDemo04{
public static void main(String args[]){
int score[] = {67,89,87,69,90,100,75,90} ; // 定義整型數(shù)組
int age[] = {31,30,18,17,8,9,1,39} ; // 定義整型數(shù)組
java.util.Arrays.sort(score) ; // 數(shù)組排序
print(score) ; // 數(shù)組打印
System.out.println("\n---------------------------") ;
java.util.Arrays.sort(age) ; // 數(shù)組排序
print(age) ; // 數(shù)組打印
}
public static void print(int temp[]){ // 輸出數(shù)組內(nèi)容
for(int i=0;i<temp.length;i++){
System.out.print(temp[i] + "\t") ;
}
}
};
輸出結(jié)果:
67 69 75 87 89 90 90 100
---------------------------
1 8 9 17 18 30 31 39
2.2、定義排序方法
可以將一個數(shù)組中的指定位置的內(nèi)容拷貝給另外一個數(shù)組羡忘,如果谎痢,此時要設(shè)計方法的話,則此方法中要傳遞多少個參數(shù)呢卷雕?
- 源數(shù)組
- 源數(shù)組的開始點
- 目標數(shù)組
- 目標數(shù)組的開始點
- 拷貝的長度
那么节猿,按照以上的思路,完成一個拷貝的操作
public class ArrayRefDemo05{
public static void main(String args[]){
int i1[] = {1,2,3,4,5,6,7,8,9} ; // 源數(shù)組
int i2[] = {11,22,33,44,55,66,77,88,99} ;// 目標數(shù)組
copy(i1,3,i2,1,3) ; // 調(diào)用拷貝方法
print(i2) ;
}
// 源數(shù)組名稱漫雕,源數(shù)組開始點滨嘱,目標數(shù)組名稱,目標數(shù)組開始點浸间,拷貝長度
public static void copy(int s[],int s1,int o[],int s2,int len){
for(int i=0;i<len;i++){
o[s2+i] = s[s1+i] ; // 進行拷貝操作
}
}
public static void print(int temp[]){ // 輸出數(shù)組內(nèi)容
for(int i=0;i<temp.length;i++){
System.out.print(temp[i] + "\t") ;
}
}
};
輸出結(jié)果:
11 4 5 6 55 66 77 88 99
對于這樣的拷貝功能太雨,在Java中也是有所支持的,直接使用Java提供的一個操作語句即可魁蒜。
public class ArrayRefDemo06{
public static void main(String args[]){
int i1[] = {1,2,3,4,5,6,7,8,9} ; // 源數(shù)組
int i2[] = {11,22,33,44,55,66,77,88,99} ;// 目標數(shù)組
System.arraycopy(i1,3,i2,1,3) ; // 調(diào)用Java中對數(shù)組支持的拷貝方法
print(i2) ;
}
public static void print(int temp[]){ // 輸出數(shù)組內(nèi)容
for(int i=0;i<temp.length;i++){
System.out.print(temp[i] + "\t") ;
}
}
};
輸出結(jié)果:
11 4 5 6 55 66 77 88 99
3躺彬、總結(jié)
- 數(shù)組的引用傳遞傳遞的就是堆內(nèi)存的使用權(quán),可以將一個數(shù)組傳遞到方法之中梅惯,傳遞的時候不需要寫上“[ ]”宪拥,直接寫名字即可。
- 方法中對數(shù)組所做的修改都會被保留下來铣减。
- 在Java中提供了一些對數(shù)組操作支持的方法她君,后續(xù)會進行介紹。