Home > Net >  Why the answer is 2 2 25 7 28
Why the answer is 2 2 25 7 28

Time:04-03

I'm currently a student studying CS in grade 10. I am wonder why this line of after executed is "2 2 25 7 28". I don't understand why the answer is that. Could anyone explain to me. Thank you

public class main {
public static void main(String[] args) {
    int[] arr = { 2, 25, 7, 28, 9 };
    for (int i = arr.length - 1; i > 0; i--)
        arr[i] = arr[i - 1];
    for (int element : arr) {
        System.out.print(element   " ");
    }
}

}

CodePudding user response:

Because you're shifting elements by one position in the array, so to speak. Index 4 in the array gets the value of index 3; index 3 gets the value of index 2, etc. Index 0 remains unaffected (because your loop condition is i > 0). So after this shifting, the array { 2, 25, 7, 28, 9 } becomes {2, 2, 25, 7, 28}. And in the end, you're looping through the elements of this new array and printing them out.

CodePudding user response:

Because that's what you wrote. Computers do what you say, not what you want.

Step through your program, either in a debugger or in your head. You have the arr array which has 2, 25, 7, 28, and 9.

In Java, array indices start at 0 till array length minus one. So arr[0] is 2, arr[1] is 25, and so on.

Your loop starts at the last array element. It sets its value to the element before that, so after the first pass, arr is 2, 25, 7, 28, and 28. The 9 has been replaced by 28. This keeps going till the 2nd element, as your loop states > 0 as the termination criteria. That means the first element in arr is not modified. Hence the output is 2 2 25 7 28.

CodePudding user response:

You have an array of length five:

At index 0 -> You have 2

At index 1 -> You have 25

At index 2 -> You have 7

At index 3 -> You have 28

and At index 4 -> You have 9

Then you have a for loop. i = 4 at first (becuase it's the length of array minus 1)

In the first iteration -> You assign the element at index 3 to the cell with index 4 so now array[4] is 28

In the second iteration -> You assign the element at index 2 to the cell with index 3 so now array[3] = 7

In the third iteration -> You assign the element at index 1 to the cell with index 2 so array[2] = 25

And in the last iteration -> You assign the element at index 0 to the cell with index 1 so array [1] = 2

and array[0] remains 2

CodePudding user response:

public static void main(String[] args) { int[] arr = { 2, 25, 7, 28, 9 }; for (int i = arr.length - 1; i > 0; i--) arr[i] = arr[i - 1]; for (int element : arr) { System.out.print(element " "); } }

since i > 0 is your ending for loop condition the first 2 in the array will not change. You are traversing the array in reverse order and replacing the last element with the one before it, then you replace the one before it with the one before that one. You keep doing that up to i = 1.

CodePudding user response:

Because of your assignment statement, you assign the previous element [i-1] to [I], but the first element does not change, so the final output is not 9, but 2, 2, 25, 7, 28

CodePudding user response:

Because you override the values to their left values, except the first.

Step 1

{ 2, 25, 7, 28, 28 };

Step 2

{ 2, 25, 7, 7, 28 };

Step 3

{ 2, 25, 25, 7, 28 };

Step 4

{ 2, 2, 25, 7, 28 };
  •  Tags:  
  • java
  • Related