Reputation: 2586
I have a 3D array, I am able to find all the index of all the array elements except one element "null"
i.e. the one after the element "x"
.
public class StringArrayTest
{
public static void main(String args[])
{
String[][][] arr ={
{
{ "a", "b" , "c"},
{ "d", "e", null }
},
{
{"x"}, null },
{{"y"}
},
{
{ "z","p"},
{}
}
};
System.out.println(arr[0][1][2]);
}
}
The question was taken from a book and the question itself was not indentated properly and it is quite confusing in the 2nd part of the array(the place where element x is).
I was able to find the index of the following element :-
a:-[0][0][0]
b:-[0][0][1]
c:-[0][0][2]
d:-[0][1][0]
e:-[0][1][1]
null:-[0][1][2]
x:-[1][0][0]
null:-Not able to find
Y:-[2][0][0]
z:-[3][0][0]
p:-[3][0][1]
What is the index value of the 2nd null, and please explain your answer.
Upvotes: 2
Views: 326
Reputation: 393841
The index of the 2nd null is [1][1]
. It's the second element of the second row.
The first element of the second row is the {"x"}
array, whose index is [1][0]
(the index of the "x" String within that array is [1][0][0]
), and the null
follows directly after it.
arr[1][1]
would return null
.
As for the table you asked for (I hope I don't have any typos) :
arr[0] => [0] => [0] => "a"
[1] => "b"
[2] => "c"
[1] => [0] => "d"
[1] => "e"
[2] => null
arr[1] => [0] => [0] => "x"
[1] => null
arr[2] => [0] => [0] => "y"
arr[3] => [0] => [0] => "z"
[1] => "p"
[1] => []
Upvotes: 3
Reputation: 615
In java multidimensional arrays are created with nested arrays. An array holding another array, making a two dimensional array. Java's array implementation is different than general. So, the null value you are not able to find have an index (1, 1). This is two dimensional because the third dimensional array is simply a value of two dimensional array and is null.
Upvotes: 1