filterIndexed
val numbers = arrayOf(10, 20, 30, 40, 50, 60)
// 筛选出索引为偶数的元素
val evenIndexedNumbers = numbers.filterIndexed { index, _ -> index % 2 == 0 }
// 输出:索引为偶数的元素: [10, 30, 50]
_console.log("索引为偶数的元素: ", evenIndexedNumbers)
val words = arrayOf("hello", "world", "kotlin", "android")
// 筛选出索引为奇数且长度大于 5 的单词
val filteredWords = words.filterIndexed { index, word ->
index % 2 != 0 && word.count() > 5
}
// 输出:筛选后的单词: [android]
_console.log("筛选后的单词: ", filteredWords)
def numbers = $objectWrappers.wrap($arrays.arrayOf($plugins.loadClass("java.lang.Integer"), 10, 20, 30, 40, 50, 60))
// 筛选出索引为偶数的元素
def evenIndexedNumbers = numbers.filterIndexed { index, _ -> index % 2 == 0 }
// 输出:索引为偶数的元素: [10, 30, 50]
$console.log("索引为偶数的元素: ", evenIndexedNumbers)
def words = $objectWrappers.wrap($arrays.arrayOf($plugins.loadClass("java.lang.String"), "hello", "world", "kotlin", "android"))
// 筛选出索引为奇数且长度大于 5 的单词
def filteredWords = words.filterIndexed { index, word ->
index % 2 != 0 && $objectWrappers.wrap(word).count() > 5
}
// 输出:筛选后的单词: [android]
$console.log("筛选后的单词: ", filteredWords)
let numbers = $objectWrappers.wrap($arrays.arrayOf($plugins.loadClass("java.lang.Integer"), 10, 20, 30, 40, 50, 60))
// 筛选出索引为偶数的元素
let evenIndexedNumbers = numbers.filterIndexed((index, _) => index % 2 == 0)
// 输出:索引为偶数的元素: [10, 30, 50]
$console.log("索引为偶数的元素: ", evenIndexedNumbers)
let words = $objectWrappers.wrap($arrays.arrayOf($plugins.loadClass("java.lang.String"), "hello", "world", "kotlin", "android"))
// 筛选出索引为奇数且长度大于 5 的单词
let filteredWords = words.filterIndexed((index, word) =>
index % 2 != 0 && $objectWrappers.wrap(word).count() > 5
)
// 输出:筛选后的单词: [android]
$console.log("筛选后的单词: ", filteredWords)
local numbers = _objectWrappers:wrap(_arrays:arrayOf(_plugins:loadClass("java.lang.Integer"), 10, 20, 30, 40, 50, 60))
-- 筛选出索引为偶数的元素
local evenIndexedNumbers = numbers:filterIndexed(function(index)
return index % 2 == 0
end)
-- 输出:索引为偶数的元素: [10, 30, 50]
_console:log("索引为偶数的元素: ", evenIndexedNumbers)
local words = _objectWrappers:wrap(_arrays:arrayOf(_plugins:loadClass("java.lang.String"), "hello", "world", "kotlin",
"android"))
-- 筛选出索引为奇数且长度大于 5 的单词
local filteredWords = words:filterIndexed(function(index, word)
return index % 2 ~= 0 and _objectWrappers:wrap(word):count() > 5
end)
-- 输出:筛选后的单词: [android]
_console:log("筛选后的单词: ", filteredWords)
<?php
$numbers = $objectWrappers->wrap($arrays->arrayOf($plugins->loadClass("java.lang.Long"), 10, 20, 30, 40, 50, 60));
// 筛选出索引为偶数的元素
$evenIndexedNumbers = $numbers->filterIndexed(function ($index) {
return $index % 2 == 0;
});
// 输出:索引为偶数的元素: [10, 30, 50]
$console->log("索引为偶数的元素: ", $evenIndexedNumbers);
$words = $objectWrappers->wrap($arrays->arrayOf($plugins->loadClass("java.lang.String"), "hello", "world", "kotlin", "android"));
// 筛选出索引为奇数且长度大于 5 的单词
$filteredWords = $words->filterIndexed(function ($index, $word) use ($objectWrappers) {
return $index % 2 != 0 and $objectWrappers->wrap($word)->count() > 5;
});
// 输出:筛选后的单词: [android]
$console->log("筛选后的单词: ", $filteredWords);
numbers = _objectWrappers.wrap(_arrays.arrayOf(_plugins.loadClass("java.lang.Long"), 10, 20, 30, 40, 50, 60))
# 筛选出索引为偶数的元素
evenIndexedNumbers = numbers.filterIndexed(lambda index, _: index % 2 == 0)
# 输出:索引为偶数的元素: [10, 30, 50]
_console.log("索引为偶数的元素: ", evenIndexedNumbers)
words = _objectWrappers.wrap(
_arrays.arrayOf(_plugins.loadClass("java.lang.String"), "hello", "world", "kotlin", "android"))
# 筛选出索引为奇数且长度大于 5 的单词
filteredWords = words.filterIndexed(lambda index, word: index % 2 != 0 and _objectWrappers.wrap(word).count() > 5)
# 输出:筛选后的单词: [android]
_console.log("筛选后的单词: ", filteredWords)
# encoding: utf-8
numbers = $objectWrappers.wrap($arrays.arrayOf($plugins.loadClass("java.lang.Long"), 10, 20, 30, 40, 50, 60))
# 筛选出索引为偶数的元素
evenIndexedNumbers = numbers.filterIndexed { |index, _| index % 2 == 0 }
# 输出:索引为偶数的元素: [10, 30, 50]
$console.log("索引为偶数的元素: ", evenIndexedNumbers)
words = $objectWrappers.wrap($arrays.arrayOf($plugins.loadClass("java.lang.String"), "hello", "world", "kotlin", "android"))
# 筛选出索引为奇数且长度大于 5 的单词
filteredWords = words.filterIndexed { |index, word|
index % 2 != 0 && $objectWrappers.wrap(word).count() > 5
}
# 输出:筛选后的单词: [android]
$console.log("筛选后的单词: ", filteredWords)
Last modified: 08 August 2025