Accessing Java array position value by index

I am really new to Java and struggling a bit with arrays. I have a block of code that I wrote following the tutorial, but I'm trying to understand it and will love it if someone can explain it to me.

I tried to work with him in different ways (explaining my duck, writing it down, etc.), but still can not circle her. I usually didn’t ask, and I always try desperately to work hard on it, but this time I just can’t understand.

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:

  • The for loop iterates through the values ​​in the "values".
  • The loop continues the loop until I become less than the last value in the array.

, values[i] System.out.println(). Java, i values[]?

, , , .

+4
5

Java , - . Java , , i. 0 less.length( 3). , 0, 1 2.

0, 1 2 :

values[0]
values[1]
values[2]
+1

JAVA , java, JAVA, :)

, values[0], values[1] ..

0
0

: :

int[] values = new int[3];

, .

i for System.out.println(values[i]);

i values[i], values[0] ..

, , , .

0

System.out.println(values[i]); .

System.out.println(values[0]);, 0, 10.

, , [1], [2]. for for(int i = 0; i < values.length; i++) i 0, values[i] values[0].

After the first run of the loop, the loop iincreases from 0 to 1 due to part of i++the for statement. in the second run values[i]there is values[1], and the value is printed at index 1, which is 20. This continues until it i < values.lenght;returns true.

0
source

Source: https://habr.com/ru/post/1629757/


All Articles