java 数组浅拷贝与深拷贝

发布时间 2023-10-29 11:16:35作者: 成佛在西天
public class demo {
    public void func(int[] nums) {
        int[] tempNums = new int[]{1, 1, 1, 1, 1, 1};
        // 浅拷贝
//        nums = tempNums;
        
        // 深拷贝
        for(int j = 0; j < nums.length; j++){
            nums[j] = tempNums[j];
        }
    }
    
    @Test
    public void main() {
        int[] nums = new int[]{0, 0, 0 ,0 , 0, 0};
        func(nums);
        for (int num : nums) {
            System.out.println(num);
        }
    }
}