+ 2
Arrays
public class Arrays { public static void main (String [] args) { int number [][]={ {3, 5 , 6 ,7, 34}, {9, 22, 25,44,70} }; System.out.println(number[0][1]); } } I’ve been trying to understand arrays in java, so far I get the basics. Can someone explain to me why the output of the code pasted above is 5?
7 Antworten
+ 8
consider 3 arrays, a,b and c
a = {3, 5,6, 7, 34}
b = {9, 22, 25, 44, 70}
the 'c' array is:
c = {a, b}
the expression says: "get the first item of c (a) and from that item get its second element (5)"
+ 3
This is a two-dimensional array (an array of arrays). So it has two "layers".
Layer 1 is an array of size 2. Each element has an array.
Index 0(number[0]): {3,5,6,7,34}
Index 1(number[1]): {9,22,25,44,70}
Layer 2 has two arrays, one for each index of layer 1. Both arrays have 5 integer numbers (indexes 0 to 4).
number[0][0]: 3
number[0][1]: 5
number[0][2]: 6
number[0][3]: 7
number[0][4]: 34
number[1][0]: 9
number[1][1]: 22
and so on...
You can make an array with many dimensions, but it is very unlikely that you will need more than 3. An example of a 3D array (an array of 2D arrays):
int[][][] my3dArray =
{
{ {8,5,4}, {3,2,9} },
{ {2,6}, {9,3}, {2} },
{ {}, {0} },
{}
} ;
number[0][0][0]: 8
number[0][1][2]: 9
number[1][0][0]: 2
number[2]: { {}, {0} }
number[2][1][0]: 0
number[3]: {} (empty array - size zero)
As you can see, any array of any layer can have its own size.
Note that trying to access number[1][1][1] or number[0][0] throws an
ArrayIndexOutOfBoundsException
+ 3
It is two dimntional array.
so we can write in matrix form as shown below
[0] [1] [2] [3] [4]
[ 0] | 3 5 6 7 34 |
[ 1] | 9 22 25 44 70 |
For number [0][1] = 5
+ 2
I wrote a small code to exemplify my answer:
https://code.sololearn.com/ciUy9U0Ihd1j/?ref=app
+ 2
Helio Oliveira Bianchi thanks alot man
0
0,22
0
For some reason I am having quite a hard time with arrays...😕