0

I'm reviewing arrays and for loops among other things in my computer science course. One of the examples used was the code below. After being run, the array displays 2, 3, 4, 2. How is this?

    int[] numbers = {1, 2, 3, 4};

    for (int i = 0; i < numbers.length; i++) {
        numbers[i] = numbers[(i+1) % numbers.length];
    }
    System.out.println(numbers);
Battle
  • 9
  • 2

5 Answers5

0

Your loop reassignes each index in your array with the value currently stored in the next index by doing (i+1).

Therefor on index [0] you get the value 2 which was on index [1] before. Same for second and third index. For the last one it is a little bit special since there is no next index the % numbers.length computation basically wraps the index so that for index [3] you will get the value of index [0] since (3+1)%4 = 0.

Note on index [3] you are getting value 2 and not 1 since you already changed the value on index [0] before that.

Sebastian
  • 47
  • 6
0

Keep in mind that % is modular arithmetic - it's the remainder when you divide the left-hand side by the right-hand side. For example, 2 % 4 = 2, 4 % 4 = 0, 6 % 4 = 2, and 8 % 4 = 0.

Here are the steps:

  1. i = 0. (i + 1) % 4 = 1 % 4 = 1. Value at index 1 is 2. Array now contains {2, 2, 3, 4}.
  2. i = 1. (i + 1) % 4 = 2 % 4 = 2. Value at index 2 is 3. Array now contains {2, 3, 3, 4}.
  3. i = 2. (i + 1) % 4 = 3 % 4 = 3. Value at index 3 is 4. Array now contains {2, 3, 4, 4}.
  4. i = 3. (i + 1) % 4 = 4 % 4 = 0. Value at index 0 is 2. Array now contains {2, 3, 4, 2}.

I'd encourage you to step through this with a debugger and/or use print statements to convince yourself that this is true and to understand why this is the case (doing so will help with understanding; if you're not convinced by a statement, either it's not true or there's something you don't understand or know about yet).

0

As you are doing the operation on the same array in place. original array [1, 2, 3, 4]

when i - 0 => number [2,2,3,4] // moving index 1 item to index 0
 when i - 2 => number [2,3,3,4] // moving index 2 item to index 1 from the 
                                  immediate previous array
  when i - 2 => number [2,3,4,4] // moving index 3 item to index 2 from the 
                                    immediate previous array
   when i - 3 => number [2,3,4,2] // moving index 0 item to index 3 from the 
                                   previous array
Uday Kumar
  • 126
  • 12
0

An important concept in understanding the code you are looking it, especially the block inside the for loop, is the modulus operator (%) also known as the remainder operator. When applied, the % operator returns the remainder of two numbers.

Hence, the computation:


(i+1) % numbers.length

will always return a remainder.

Walking through it with a debugger (or print statements as suggested) while evaluating the values (and operations) at each iteration is a great way of understanding it.

You can also read more about it: https://www.baeldung.com/modulo-java

k.wahome
  • 962
  • 5
  • 14
-1

1st iteration numbers[i] = numbers[1 % 4]; // answer is 1 so numbers[1] = 2

2nd iteration numbers[i] = numbers[2 % 4]; // answer is 2 so numbers[2] = 3

Like this it will go until I becomes 3

  • This answer doesn't offer much explanation - and you assume the only issue the OP is having is not knowing how indexes work. – sleepToken Jan 16 '20 at 17:55