数组作为方法参数的传值问题

package com.imooc.method;

public class ArrayDemo {
    //定义一个用于修改某个数组元素值的方法:
    public void updateArray(int[] a) {
        a[3]=15;
        System.out.println("数组a的元素为:");
        for(int n:a) {
            System.out.print(n+" ");
        }
        System.out.println();
    }
    public static void main(String[] args) {
        ArrayDemo ad=new ArrayDemo();
        int[] a1= {1,2,3,4,5};
        System.out.println("方法调用前数组a1的元素为:");
        for(int n:a1) {
            System.out.print(n+" ");
        }
        System.out.println();
        ad.updateArray(a1);
        System.out.println("方法调用后数组a1的元素为:");
        for(int n:a1) {
            System.out.print(n+" ");
        }
    }

}
/*
 * 输出结果:
 * 方法调用前数组a1的元素为:
1 2 3 4 5 
数组a的元素为:
1 2 3 15 5 
方法调用后数组a1的元素为:
1 2 3 15 5 
 * */
 */

数组传值是引用数据类型的传值,方法改变数组中的元素,主方法中的数组元素也会改变

posted @ 2019-12-04 06:53  生如逆旅,一苇以航  阅读(1393)  评论(0编辑  收藏  举报