Problem with assigning an array to other array in Java

前端 未结 4 570
情歌与酒
情歌与酒 2020-11-27 19:20
public class TestingArray {

    public static void main(String[] args) {

        int iCheck = 10;
        int j = iCheck;
        j = 11;
        System.err.printl         


        
4条回答
  •  悲&欢浪女
    2020-11-27 20:25

    Simply put, "val1" and "val2" are pointers to the actual array. You're assigning val2 to point to the same array as val1. Therefore, change one, and the other sees the same change. To have it truly be a copy, you'd have to clone the array instead of assigning.

提交回复
热议问题