0

Why the output of this code is

island = Fiji
island = Cozumel
island = Bermuda
island = Azores

Why the output starts from Fiji island , instead of "Bermuda"? Bermuda have 0 element in array . Can you please point me why my output have such specific order.

public class TestArrays {
public static void main (String[] args){
    int y = 0;
    int[] index = new int [4];
    index[0] = 1;
    index[1] = 3;
    index[2] = 0;
    index[3] = 2;


    String[] islands = new String[4];
    islands[0] = "Bermuda";
    islands[1] = "Fiji";
    islands[2] = "Azores";
    islands[3] = "Cozumel";

    int ref;
    while ( y < 4){
        ref = index[y];
        System.out.print("island = ");
        System.out.println(islands[ref]);
        y = y + 1;

    }

}
2
  • As i understand it should be like : Bermuda / Fiji / Azores / Cozumel Each time by adding y+1; Commented Sep 18, 2016 at 18:49
  • That would be the case when printing islands[y]. Commented Sep 18, 2016 at 18:54

5 Answers 5

1

It's quite simple:

ref = index[y];

The above line outputs ref as 1, since index[y] results 1 when y was 0.

Therefore,

ref = 1  //index[0] = 1;

Now, the line below:

System.out.println(islands[ref]);

Outputs:

island = Fiji

because ref was 1 and islands[ref] stands Fiji as in:

islands[1] = "Fiji";
Sign up to request clarification or add additional context in comments.

Comments

0

Your index is ordered as {1, 3, 0, 2}

int[] index = new int [4];
index[0] = 1;
index[1] = 3;
index[2] = 0;
index[3] = 2;

So it will print islands[1], islands[3], islands[0], islands[2]

String[] islands = new String[4];
islands[0] = "Bermuda";
islands[1] = "Fiji";
islands[2] = "Azores";
islands[3] = "Cozumel";

Fiji, Cozumel, Bermuda, Azores

Comments

0

Basically, it is printing out that way because you are telling it to.

You set up your index like this

int[] index = new int [4];
    index[0] = 1;
    index[1] = 3;
    index[2] = 0;
    index[3] = 2;

and then call

ref = index[y];
        System.out.print("island = ");
        System.out.println(islands[ref]);
        y = y + 1;

The value in index[y] is why you are printing it out in that order.

index[0] is 1, so when you call System.out.println(islands[ref]); you are calling System.out.println(islands[1]);, which is Fiji.

Comments

0

In the call island[ref] for y=0,1,2,3. The calls are

island[index[0]] = island [1] = "Fiji"
island[index[1]] = island [3] = "Cozumel"
island[index[2]] = island [0] = "Bermuda"
island[index[3]] = island [2] = "Azores"

You have basically changed the index call for island from your index array.

Comments

0
    int ref;
    while ( y < 4){
        System.out.println(y); (1)
        ref = index[y];
        System.out.println(ref); (2)
        System.out.print("island = ");
        System.out.println(islands[ref]);
        y = y + 1;

    }

Iteration # 01

Statement (1) will produce 0 since y is 0, and statement (2) will produce 1 because index[0] has 1 stored in it. And islands[1] has Fiji

Iteration # 02

Statement (1) will produce 1 since y is 1, and statement (2) will produce 3 because index[1] has 3 stored in it. And islands[3] has Cozumel

Iterating over the while loop like this until y < 4 for each index will help you see, why the generated output is like the way, it is.

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.