簡體   English   中英

查找沒有循環的數組中相同元素的索引

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM