Why is the output of this code 111111?

后端 未结 9 1978
星月不相逢
星月不相逢 2021-01-13 20:10

This is the code. I tried to solve it, but I can\'t understand how its output is 111111?

public class Test { 
      public static void main(String[] args) {
         


        
9条回答
  •  醉话见心
    2021-01-13 20:39

    int list[] = {1, 2, 3, 4, 5, 6}; 
    
     for (int i = 1; i < list.length; i++) {
             list[i] = list[i - 1];
     }
    

    When i is 1, you assign list[1] = list[0], here list[0] value is 1

    Then array is updated {1, 1, 3, 4, 5, 6}, loop again

    When i is 2, you assign list[2] = list[1], here list[1] value is 1

    Then array is updated {1, 1, 1, 4, 5, 6}, loop again

    In that way, array is override {1, 1, 1, 1, 1, 1}

提交回复
热议问题