0

hope you are well. This is my first post here with some basic question about Array in Java.

int arr[] = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int n = 6;
n = arr[arr[n]];
System.out.println(n);

Someone can explain this to me, why the output is 8?

1
  • 2
    Try to understand that by using one more variable - assign arr[n] to v or something and see what is going on. Commented May 13, 2020 at 12:01

6 Answers 6

3

As you are using following code :

        int arr[] = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int n = 6;
        n = arr[arr[n]];
        System.out.println(n);

By the above code

n=arr[arr[6]];

the value of arr[6]=7

n=arr[7];

As we can see the value of arr[7] = 8

Sign up to request clarification or add additional context in comments.

Comments

2
int arr[] = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        int n = 6;
        n = arr[arr[n]];
        System.out.println(n);

arr[n] with n == 6 -> the 7th element of your array, which is 7 (array indices are 0 based) arr[7] -> the 7th element of your array, being 8

3 Comments

Right! I tried to solve this only with one Array, but didn't consider the second one! Now it's seems to be clear!
I mean with code n = arr[arr[n]]; I need to check one Array twice. Hope I wrote it well this time :)
Yes, that code is actually equivalent to: int x = arr[n]; int n = arr[x];
2

First it will calculate the value of inner arr[n] which is arr[6], the value of this will be 7. Next it will substitute this value and your expression will evaluate to arr[7] which is 8.

Hence the final answer is 8.

The evaluation sequence is inner to outer.

Comments

1
    n = arr[arr[n]];

is equivalent to

    n = arr[n]; // start with index 6 and pick value 7
    n = arr[n]; // start with index 7 and pick value 8

Comments

1

Concentrate this line n = arr[arr[n]]; n=6 let int temp=arr[n]; so n=arr[temp]; as n=6 temp=arr[6]=7; temp=7; Now n=arr[temp]=arr[7]=8;

Array start with 0 so arr[0]=1,arr[1]=2 ... arr[6]=7

Final output arr[7]=8

Comments

0

Try this and see if it helps. The --> should be read as the same as.

int arr[] = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
for (int n = 0; arr[n] < arr.length; n++) {
       System.out.println("n = " +n + ", arr[" + n + "] = " + arr[n] + ", arr[arr[" + n +"]] --> arr[" + arr[n] + "] = " +arr[arr[n]]);
}

prints

n = 0, arr[0] = 1, arr[arr[0]] --> arr[1] = 2
n = 1, arr[1] = 2, arr[arr[1]] --> arr[2] = 3
n = 2, arr[2] = 3, arr[arr[2]] --> arr[3] = 4
n = 3, arr[3] = 4, arr[arr[3]] --> arr[4] = 5
n = 4, arr[4] = 5, arr[arr[4]] --> arr[5] = 6
n = 5, arr[5] = 6, arr[arr[5]] --> arr[6] = 7
n = 6, arr[6] = 7, arr[arr[6]] --> arr[7] = 8
n = 7, arr[7] = 8, arr[arr[7]] --> arr[8] = 9

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.