zoukankan      html  css  js  c++  java
  • Java学习---- 数组的引用传递

    1.

    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、

    2.

    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

    3.

    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(" ---------------------------") ;
    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] + " ") ;
    }
    }
    };

    运行结果:

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

    4.

    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(" ---------------------------") ;
    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] + " ") ;
    }
    }
    };

    运行结果:

    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] + " ") ;
    }
    }
    };

    运行结果:

    11 4 5 6 55 66 77 88 99

    6.

    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] + " ") ;
    }
    }
    };

    运行结果:

    11 4 5 6 55 66 77 88 99

  • 相关阅读:
    【设计模式
    【JavaEE】之SSM入门项目的搭建
    【Android
    【Android
    【Android
    【Android
    【Android
    【Android
    随风 随意
    优秀代码所具备的品质
  • 原文地址:https://www.cnblogs.com/fenr9/p/5369220.html
Copyright © 2011-2022 走看看