2

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.

penta
  • 2,536
  • 4
  • 25
  • 50

2 Answers2

3

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] => []
Eran
  • 387,369
  • 54
  • 702
  • 768
  • hey, thanks for your answer, can you tell me why System.out.println(arr[1][1][1]); cannot be used ? – penta Nov 16 '15 at 11:05
  • Your answer is correct, but the array is 3d array and you called a 2d array, why is it able to execute the 2d array, i mean why can't the answer be in 3d array – penta Nov 16 '15 at 11:07
  • @penta Your 3D array is not fully initialized, since `arr[1][1]` is null. Therefore you can't access `arr[1][1][i]` for any value of `i`. Note that a 3D array is actually a 2D array whose elements are themselves arrays, and they can be null (as any reference type can be). – Eran Nov 16 '15 at 11:10
  • I wil accept the answer in few minutes, can you please make a table as to how the elements are arranged, just a request :-) – penta Nov 16 '15 at 11:11
  • OMG, thanks, its totally clear now, thanks a yottabyte :-D – penta Nov 16 '15 at 11:43
1

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.

sohan nohemy
  • 615
  • 5
  • 13