1

I have this array exercise. I want to understand how things work, if someone can

  1. we have the object array of type int called index with 4 elements
  2. we have the object array of type String called islands with 4 elements

I don't understand how things are passing to each other, I need a good explanation.

class Dog {
  public static void main(String [] args) {
    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 y = 0;
    int ref;

    while (y < 4) {
      ref = index[y];
      System.out.print("island = ");
      System.out.println(islands[ref]);
      y ++;
    }
  }
4
  • 3
    What is your "Specific" question? Commented Dec 22, 2012 at 10:56
  • 2
    Consider using a for loop like for (int y = 0; y < 4; y++) instead of your while loop. Just a better style. Anyway what is your question! Commented Dec 22, 2012 at 11:01
  • Also note that "homework" tag has been officially deprecated Commented Dec 22, 2012 at 11:02
  • 2
    You would need to specify your question a bit better in order to get very good and accurate answer. Otherwise we could write half of a book explaining fundamentals of Java Commented Dec 22, 2012 at 11:04

4 Answers 4

6

Take a pen and paper and make a table like this, and go through the iterations:

 y    ref    islands[ret]
---   ---    ------------
 0     1      Fiji
 1     3      Cozumel
 2     0      Bermuda
 3     2      Azores
Sign up to request clarification or add additional context in comments.

Comments

0

Well, you've added index in the array named index then accessing the same value in while loop

int y=0;
ref = index[y]; // now ref = 1
islands[ref] // means islands[1] which returns the value `Fiji` that is stored in 1st position

Comments

0

First, you make an array that holds ints, the array has length 4 (4 variables can be in it):

int [] intArray = new int[4];

Your array is called index which may be confusing for the explanation. The index of an array is which "position" you are referring to and is between 0 and the length-1 (inclusive). You can use it in two ways:

int myInt = intArray[0]; //get whatever is at index 0 and store it in myInt
intArray[0] = 4; //store the number 4 at index 0

The following code does nothing more than get a number from the first array and use it to access a variable in the second array.

ref = index[y];
System.out.println(islands[ref])

Comments

0

To make it understand, take a paper and pen and jot down the arrays in table form and iterate through the loop to understand. (back in school days, our teacher(s) called it a dry run)

First we represent the data

Iteration    y           ref ( `ref = index[y]`)  islands
  1          0                 1                    Fiji 
  2          1                 3                   Cozumel
  3          2                 0                   Bermuda
  4          3                 2                    Azores

So you can go through the iterations Iteration 1

y=0
ref = index[y], i.e. index[0] i.e 1
System.out.print("island = "); prints island = 
System.out.println(islands[ref]); islands[ref] i.e islands[1] i.e Fiji

hence for iteration 1 output will be

island = Fiji

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.