In int x = sample [1] [0]; of array, why [1] has not been executed? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
- 1

In int x = sample [1] [0]; of array, why [1] has not been executed?

It`s only executing [0] which has a value 4. But why not the first array?

6th Dec 2016, 11:03 AM
Junaid B
Junaid B - avatar
4 Answers
+ 2
Its only printing "4" because you're just printing that element at the specifed index. If you want to print all the data i recommend you use a nested for loop. Also be careful of going arrayIndexOutOFBounds because some "array of arrays" have different lengths.
6th Dec 2016, 1:36 PM
Ousmane Diaw
+ 1
int x = myArr [1] [0]; Arrays starts with position 0. [1] refers to the 2nd position of your myArr. In this case, {4}. [0] refers to the 1st position of myArr [1] (i.e.: {4}) which is 4. hence, 4 is being executed if u want x to be an integer from the first array (i.e.: 1, 2 or 3) then you can change the value of int x to the following: int x = myArr[0][0] for x = 1 int x = myArr[0][1] for x = 2 int x = myArr[0][2] for x = 3
7th Dec 2016, 8:13 AM
lowshuen
0
can you put program snippet in details?
6th Dec 2016, 11:46 AM
Nagendra Prajwal
Nagendra Prajwal - avatar
0
public class Program { public static void main(String[] args) { int[ ][ ] myArr = { {1, 2, 3}, {4}, {5, 6, 7} }; myArr[0][2] = 42; int x = myArr[1][0]; System.out.println(x); } }
6th Dec 2016, 12:00 PM
Junaid B
Junaid B - avatar