Arrays | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

29th Jan 2019, 12:09 PM
Kelvin A
Kelvin A - avatar
7 Answers
+ 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)"
29th Jan 2019, 12:38 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 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
29th Jan 2019, 1:26 PM
Helio Oliveira Bianchi
Helio Oliveira Bianchi - avatar
+ 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
21st Dec 2019, 1:29 AM
Cran
Cran - avatar
+ 2
I wrote a small code to exemplify my answer: https://code.sololearn.com/ciUy9U0Ihd1j/?ref=app
29th Jan 2019, 1:53 PM
Helio Oliveira Bianchi
Helio Oliveira Bianchi - avatar
+ 2
Helio Oliveira Bianchi thanks alot man
29th Jan 2019, 1:55 PM
Kelvin A
Kelvin A - avatar
0
0,22
18th Mar 2020, 7:05 AM
KUMAAR
KUMAAR - avatar
0
For some reason I am having quite a hard time with arrays...😕
18th Aug 2020, 3:43 PM
Jason
Jason - avatar