Why is the output of this code 111111?

后端 未结 9 1942
星月不相逢
星月不相逢 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:49

    for (int i = 1; i < list.length; i++)
              list[i] = list[i - 1];
    

    This part of your code causes value of all items in list is 1.

    When i = 1, list[1] = list[0] = 1;
    when i = 2, list[2] = list[1] = 1; //not 2 because it is already overrided when i = 1.
    

    .... to last one.

    P/S: Try to understand step by step in your code before asking question.

提交回复
热议问题