在没有循环

时间:2015-11-19 07:39:05

标签: java arrays duplicates java-6

假设我有以下数组:

arr = [T, F, F, T, F, F, F, T, F];

有没有简单的方法来查找T的索引?我想在这种情况下返回[0,3,7]并且不想使用任何循环。

我在网上搜索了一个解决方案,但找不到任何内容。

1 个答案:

答案 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]