Sumit Kumar Sumit Kumar - 5 months ago 17
PHP Question

How can I define such an array?

How can I define such an array?

I need to make an array in JAVA like the one below:

Array(
[0]=> Array(
[0]=>1
[1]=>1
[2]=>1
)
[1]=> Array(
[0]=>4
[1]=>7
[2]=>10
)
)


And how do I read this array using JAVA?

The PHP equivalent of this would be:

$a=array( array( 1, 1, 1 ), array( 4, 7, 10 ) );


And I can read it like this in PHP:

foreach( $a as $v ){
echo $v[0]. " ". $v[1]." ". $v[2]. "\r\n";
}


To sum up, I want to define an INT array of size 2, and each element of this array is another array of size 3.
Then I want to read the inside array 3 values during a loop in three different INT variables.
How can I do this?

Answer

You can use { } to define arrays

int[][] values = {{ 1, 1, 1 }, { 4, 7, 10 }};

To print them in a loop like you do

for (int[] a : values) 
    System.out.println(a[0] + " " + a[1] + " " + a[2]);

I suggest you read up on how arrays work in Java.

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html