简体   繁体   English

从给定的3D数组中找到null的数组索引

[英]Find the array Index of null from the given 3D array

I have a 3D array, I am able to find all the index of all the array elements except one element "null" ie the one after the element "x" . 我有一个3D数组,我能够找到所有数组元素的所有索引,除了一个元素"null"即元素"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). 该问题来自一本书,该问题本身未正确缩进,并且在数组的第二部分(元素x所在的位置)相当混乱。

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. 第二个null的索引值是多少,请解释您的答案。

The index of the 2nd null is [1][1] . 第二个null的索引为[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. 第二行的第一个元素是{"x"}数组,其索引为[1][0] (该数组内“ x”字符串的索引为[1][0][0] ),并且null紧随其后。

arr[1][1] would return null . arr[1][1]将返回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] => []

In java multidimensional arrays are created with nested arrays. 在Java中,多维数组是使用嵌套数组创建的。 An array holding another array, making a two dimensional array. 一个包含另一个数组的数组,构成一个二维数组。 Java's array implementation is different than general. Java的数组实现与常规实现不同。 So, the null value you are not able to find have an index (1, 1). 因此,您找不到的空值具有索引(1,1)。 This is two dimensional because the third dimensional array is simply a value of two dimensional array and is null. 这是二维的,因为三维数组只是二维数组的值,并且为空。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM