繁体   English   中英

如何从嵌套数组中获取值或建立索引并比较条件

[英]how to get the values or indexed from nested array and compare conditions

如何分别显示值john或mary或​​alice或bob? 从示例嵌套数组。

String[][] kstemmers = new String[][] {
    {"John", "Mary"}, 
    {"Alice", "Bob"}
};

将2d数组视为一个表(列和行)。

因此,基本上,您有:

John Mary
Alice Bob

现在,使用这些列和行将其转换为表,您将获得如下内容:

     0       1
  ________________
0 | John  | Mary |
1 | Alice | Bob  |

因此,要获取“玛丽”,您可以使用kstemmers[0][1]

有可用的Java 8流解决方案,但认为最好在提供2D数组之前先了解2D数组的基本概念。

在Java 8中使用Stream:

    Arrays.stream(kstemmers)
            .map(kstemmer -> String.join(" ", kstemmer))
            .forEach(System.out::println);

使用java8“ flatMap”来玩。 一种方法可能是跟随

List<String> collection = Arrays.stream(kstemmers).flatMap(Arrays::stream).collect(Collectors.toList());

然后遍历List

暂无
暂无

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

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