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


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 
 * */
 */

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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM