5

I'm really new to Java and am just struggling with arrays a little bit. I've got a block of code that I've written when following a tutorial but am struggling to understand it and would love if someone could explain it to me.

I've tried working through it with various different methods (explaining to my duck, writing it down, etc.) and still can't get my head around it. I normally wouldn't ask and I always try desperately hard to work it out myself, but I just can't figure it out this time.

int[] values = new int[3];

values[0] = 10;
values[1] = 20;
values[3] = 30;

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

I understand why:

  1. The for loop iterates through the values in "values".
  2. The loop keeps looping until i is less than the last value in the array.

But what I don't understand is why I need to write values[i] in the System.out.println() statement. What tells Java that i can be used in the array values[]?

Sorry if this is a trivial question for you but this is the best place I could think of to turn.

4
  • 2
    Do you really have a pet duck? :O Commented Feb 21, 2016 at 9:44
  • Because the square brackets [index] accesses the element of the array at the index. Commented Feb 21, 2016 at 9:45
  • 2
    You'll get an ArrayOutOfBoundsException on row values[3] = 30;, since 3 points to 4th array element. Commented Feb 21, 2016 at 9:46
  • There's an error in the 3rd assignment. It should be values [2] instead of values[3], because Java arrays are 0-indexed and so an index of 3 indicates the 4th array elegant, which dies not exist as values was assigned to be a 3-element array, and otherwise you would get an ArrayIndexOutOfBoundsException. Commented Feb 21, 2016 at 10:28

5 Answers 5

1

Java knows that values is an array type. Arrays in Java are indexed by integers, so here we have an integer called i. i goes from 0 to less than values.length (in this case is 3). So i will be 0, 1, and 2.

Indexing values with 0, 1, and 2 are equivalent to:

values[0]
values[1]
values[2]
Sign up to request clarification or add additional context in comments.

Comments

0

Since JAVA is not a mind reader, unless you tell java value of which index you want to print JAVA will not be able to understand :)

So you have mention index like values[0] ,values[1] etc

Comments

0

values is the array name and 'i' is the index. values[0] will print 10. values[1] will print 20.

Refer these links for more information:

Also learn how to debug the java code.

Comments

0

Let me try to explain in a simple way: As soon as you use the statement:

int[] values = new int[3];

and assign values to it there will be three blocks of memory of integer size in memory.

as i is initialized to zero and each iteration in the for loop, will execute the statement System.out.println(values[i]);

It will just substitute current value of i in values[i] that is values[0] for first round etc.

So it will check for already assigned values in the memory I talked about earlier and print it.

Comments

0

The System.out.println(values[i]); Tells the system to print the value at the given index in the array.

If we say System.out.println(values[0]);, the value at index 0 which is 10 is printed.

To print out all the values in the array, instead of typing the index for each value manually like values[1], values[2]. We use a for statement for(int i = 0; i < values.length; i++) creates a variable i and assigns it a value 0, so on the first run through the loop,values[i] is values[0].

After the first run through the loop, the i is incremented from 0 to 1 because of the i++ part of the for statement. on the second run values[i] is values[1] and what is printed is the value at index 1 which is 20. This continues as long as i < values.lenght; returns true.

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.