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.

2 Answers 2

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] => []
Sign up to request clarification or add additional context in comments.

5 Comments

hey, thanks for your answer, can you tell me why System.out.println(arr[1][1][1]); cannot be used ?
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 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).
I wil accept the answer in few minutes, can you please make a table as to how the elements are arranged, just a request :-)
OMG, thanks, its totally clear now, thanks a yottabyte :-D
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.