[英]Find indices of the same elements in an array without loop
假設我有以下數組:
arr = [T, F, F, T, F, F, F, T, F];
是否有找到T
的索引的簡便方法? 在這種情況下,我想返回[0,3,7]
,並且不想使用任何循環。
我在網上搜索了一個解決方案,但找不到任何東西。
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]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.