假设我有以下数组:
arr = [T, F, F, T, F, F, F, T, F];
有没有简单的方法来查找T
的索引?我想在这种情况下返回[0,3,7]
并且不想使用任何循环。
我在网上搜索了一个解决方案,但找不到任何内容。
答案 0 :(得分:0)
List<String> list = Arrays.asList("T", "F", " F", "T", "F", "F", "F", "T", "F");
String looking_for = "T";
int[] indices =
IntStream.range(0, list.size())
.filter(i -> list.get(i).equals(looking_for)).toArray();
System.out.printf("Indices with %s are %s%n", looking_for, Arrays.toString(indices));
输出:
Indices with T are [0, 3, 7]