在匹配条件时从数组中获取所有索引

Get all the indexes from the array on matching the condition

我有一个从 preparedStatement 中 return 编辑的 int 数组,如下所示:

int[] updateResultArray = preparedStatement.executeBatch();

根据我的理解,上面的 returned 数组将包含在该批次上添加的每个更新查询中受到影响的行数。 因此,结果将类似于 [0,2,1,0,0,1,2],这里我只想获取值大于零的索引。 我的预期结果应该 return 索引 1,2,5,6。我知道我们可以将其转换为列表并执行以下代码:

int[] indexes =
                    IntStream.range(0, names.size())
                             .filter(i -> names.get(i)>0)
                             .toArray();

但是没有这种转换,还有其他方法吗?谁能给我一些解决方案。

您不需要将数组转换为List来进行此操作,您只需:

int[] indexes = IntStream.range(0, updateResultArray.length)
        .filter(i -> updateResultArray[i] > 0)
        .toArray();