第四章:数组的引用传递

来源:互联网 发布:2016中国电梯行业数据 编辑:程序博客网 时间:2024/06/05 23:57

数组的引用传递
一. 传递及返回数组
如果要向方法传递一个数组,则方法的接受参数必须是符合其类型的数组。而且数组引用属于引用数据类型,所以在把数组传递进方法之后,如果方法对数组本身做了任何修改,修改结果也将保留下来。
【向方法中传递数组】

public class ArrayRefDemo01{    public static void main(String args[]){        int temp[] = {1,3,5} ;      // 利用静态初始化方式定义数组        fun(temp) ;                 // 传递数组        for(int i=0;i<temp.length;i++){            System.out.print(temp[i] + "、") ;        }    }    public static void fun(int x[]){    // 接收整型数组的引用        x[0] = 6 ;          // 修改第一个元素    }};

运行结果为:

6、3、5、

在fun方法中修改了数据并保存了下来。向方法中传递数组的过程如下:
这里写图片描述
从图中可以看出,一开始声明的temp数组的内容是“1.3.5”,但是将此数组传递到了方法中,并使用数组X接收,也就是说此时temp实际上是将堆内存的使用权传递给了方法,为数组的具体内容起了一个别名X,然后在方法中通过X修改数组中的内容,方法执行完毕后,数组x因为是局部变量所以就失效了,但是对于数组内容的修改却保留了下来,值就是数组引用传递的过程。关于引用数组类型的引用传递,在面向对象部分会更加完整的介绍。
【使用方法返回一个数组】

public class ArrayRefDemo02{    public static void main(String args[]){        int temp[] = fun() ;        // 通过方法实例化数组        print(temp) ;               // 打印数组内容    }    public static void print(int x[]){        for(int i=0;i<x.length;i++){            System.out.print(x[i] + "、") ;        }    }    public static int[] fun(){  // 返回一个数组        int ss[] = {1,3,5,7,9} ;    // 定义一个数组        return ss ;    }};

运行结果为:

1、3、5、7、9、

二. 数组排序
【数组排序】

public class ArrayRefDemo03{    public static void main(String args[]){        int score[] = {67,89,87,69,90,100,75,90} ;  // 定义整型数组        int age[] = {31,30,18,17,8,9,1,39} ;        // 定义整型数组        sort(score) ;       // 数组排序        print(score) ;      // 数组打印        System.out.println("\n---------------------------") ;        sort(age) ;         // 数组排序        print(age) ;        // 数组打印    }    public static void sort(int temp[]){        // 执行排序操作        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[]){       // 输出数组内容        for(int i=0;i<temp.length;i++){            System.out.print(temp[i] + "\t") ;        }    }};

程序运行结果:

67      69      75      87      89      90      90      100---------------------------1       8       9       17      18      30      31      39

对于排序操作,在Java本身也是有类库支持的,可以直接使用“java.util.Arrays.sort(数组名称)”对数组进行排序:
【使用类库进行排序】

public class ArrayRefDemo04{    public static void main(String args[]){        int score[] = {67,89,87,69,90,100,75,90} ;  // 定义整型数组        int age[] = {31,30,18,17,8,9,1,39} ;        // 定义整型数组        java.util.Arrays.sort(score) ;      // 数组排序        print(score) ;      // 数组打印        System.out.println("\n---------------------------") ;        java.util.Arrays.sort(age) ;            // 数组排序        print(age) ;        // 数组打印    }    public static void print(int temp[]){       // 输出数组内容        for(int i=0;i<temp.length;i++){            System.out.print(temp[i] + "\t") ;        }    }};

程序运行结果:

67      69      75      87      89      90      90      100---------------------------1       8       9       17      18      30      31      39

此方法可以直接对各个基本数据类型的数组进行排序,包括浮点类型、字符型等
三. 数组复制
如果给定两个数组,将其中一个数组指定位置复制给另一个数组,可以用方法来完成。在方法中接收5个参数,分别是“原数组名称”、“原数组开始点”、“目的数组名称”、“目的数组开始点”、“复制长度”
【数组复制】

public class ArrayRefDemo05{    public static void main(String args[]){        int i1[] = {1,2,3,4,5,6,7,8,9} ;        // 源数组        int i2[] = {11,22,33,44,55,66,77,88,99} ;// 目标数组        copy(i1,3,i2,1,3) ; // 调用拷贝方法        print(i2) ;    }    // 源数组名称,源数组开始点,目标数组名称,目标数组开始点,拷贝长度    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[]){       // 输出数组内容        for(int i=0;i<temp.length;i++){            System.out.print(temp[i] + "\t") ;        }    }};

运行结果:

11      4       5       6       55      66      77      88      99

同理,复制数组在Java类库中也是支持的,使用System.arraycopy()方法即可,此方法也要接收参数,参数的顺序和上面的范例的copy相同。
【类库中的方法进行复制操作】

public class ArrayRefDemo06{    public static void main(String args[]){        int i1[] = {1,2,3,4,5,6,7,8,9} ;        // 源数组        int i2[] = {11,22,33,44,55,66,77,88,99} ;// 目标数组        System.arraycopy(i1,3,i2,1,3) ; // 调用Java中对数组支持的拷贝方法        print(i2) ;    }    public static void print(int temp[]){       // 输出数组内容        for(int i=0;i<temp.length;i++){            System.out.print(temp[i] + "\t") ;        }    }};

程序运行结果:

11      4       5       6       55      66      77      88      99

Java中的类库:
在Java中提供了大量的类库,这些类库可以直接从java.doc中查找,但是在此处没有必要研究doc文档,最好在学习完面向对象之后再进行研究。

0 0