在Dart/Flutter中,要确定一个二维列表(嵌套列表)是否包含另一个列表,可以通过遍历二维列表并逐一比较每个子列表来实现。以下是一个示例代码,展示了如何进行这种检查:
bool containsList(List<List<int>> twoDList, List<int> targetList) {
for (var subList in twoDList) {
if (subList.length == targetList.length) {
bool isMatch = true;
for (int i = 0; i < subList.length; i++) {
if (subList[i] != targetList[i]) {
isMatch = false;
break;
}
}
if (isMatch) {
return true;
}
}
}
return false;
}
void main() {
List<List<int>> twoDList = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
];
List<int> targetList = [4, 5, 6];
if (containsList(twoDList, targetList)) {
print('The two-dimensional list contains the target list.');
} else {
print('The two-dimensional list does not contain the target list.');
}
}
[[1, 2], [3, 4]]
是一个二维列表。==
方法来进行深度比较。containsList
函数接受一个二维列表和一个目标列表作为参数。true
,否则返回 false
。通过这种方式,可以有效地确定一个二维列表是否包含特定的子列表。
领取专属 10元无门槛券
手把手带您无忧上云