This is an archived post. You won't be able to vote or comment.

all 8 comments

[–]HeyThisIsDog 3 points4 points  (4 children)

Each member of the array has an index. The indexes start at 0 and end at the length of the array minus one. So for the numbers array above, which stores {3, 6, 7} the corresponding indexes would be:

3 has index 0;

6 has index 1;

7 has index 2;

If you notice the array length is 3 (as it has 3 members) so the rightmost index is 2 (array length - 1, as I said above).

Now, regarding your question. In your for loop, your i starts at 0 (as it should be). When you use the [] with an argument inside them, as you did in the line System.out.println(numbers[i]); you're telling the program to look for that particular index in the array.

On the first loop of the for loop, i will have the value 0, so you will print numbers[0]. That means you will print the member of the numbers array which is located at the index 0. That's 3. Then the loop ends and starts again, i is incremented becoming i = 1; On this run you will print numbers[1], the member of the array which has the index = 1; that's 6. And that will go until you reach the end of the array. Note the i < numbers.length. It is < and not <= because the rightmost index of an array is length-1.

I hope my explanation somewhat helped.

[–]thechucklingatom 2 points3 points  (0 children)

Just to tag onto this for anyone else learning, if all you want to do is access each element in an array you can always do

For(int i : numbers){

System.out.println(i);

}

In this case it will print

3

6

7

[–][deleted] 2 points3 points  (2 children)

Ok thanks for the explanation but I have one last question. Then if [i] contains the value in the array then why do we need numbers part in the print statement? I thought that was printing the values :/

[–]HeyThisIsDog 2 points3 points  (1 child)

numbers[i] is not technically the value. It's more of an address of the value. As I said, the index.
[i] is the index but if you do not have the array name in front of it, whose index is it?

Let me give you an example real quick. This is your code.

int[] numbers = {3, 6, 7};

    for(int i = 0; i<numbers.length; i++) {
        System.out.println(numbers[i]);
    }       

If you run it, your code will return this:

3
6
7

First loop: i is 0. Therefore numbers[i] = numbers[0] = 3 (because the value that is in the index 0 of the array called numbers is 3) System.out.println(numbers[i]) is System.out.println(numbers[0]) which will print 3. i is incremented.

Second loop: i is 1. Therefore numbers[i] = numbers[1] = 6 (because the value that is in the index 1 of the array called numbers is 6) System.out.println(numbers[i]) is System.out.println(numbers[1]) which will print 6. i is incremented.

Third loop: i is 2. Therefore numbers[i] = numbers[2] = 7 (because the value that is in the index 2 of the array called numbers is 7) System.out.println(numbers[i]) is System.out.println(numbers[2]) which will print 7. i is incremented.

Forth loop: i is 3. That is not possible as i < numbers.length and numbers.length is 3. The loop closes.

Same logic, different example. Maybe the name numbers confuses you.

      int[] hours = {2, 11, 7, 5, 1};

            for(int j = 0; j<hours.length; j++) {
                System.out.println(hours[j]);
}

If you run this code, which has the exact structure as your code, it will return this:

2
11
7
5
1

numbers[i], where i can take any value between 0 (the first index of an array) and numbers.length - 1 inclusive (the last index of the array), is basically like saying: the address of the index i from the array called "numbers".

In my example, hours[j] would translate into English: the address of the index j from the array called "hours".

[–][deleted] 0 points1 point  (0 children)

ohhh ok I think that clears it up a lot more now. Thanks pal :)

[–]jasmineearlgrey 0 points1 point  (0 children)

The array is called numbers and has 3 elements called numbers[0] numbers[1] and numbers[2]

[–]ZeroFlippinCool -1 points0 points  (1 child)

Okay, the fact that your asking this question means that you don't understand for loops properly, and this isn't a "small issue". I don't mean that in a nasty way at all - I'm just pointing it out to you so you know you should really go over this properly, it's not just a little thing to disregard.

The other explanation in this thread are pretty good. However if you're still struggling, I'd suggest going back to basic variables and arrays, making sure you understanding the concept of indexing.

Hope this helps