static boolean checkIndex ( List<List<Integer>> indexList, List<Integer> listToCheck ){
for (List<Integer> indices : indexList) {
int val = listToCheck.get( indices.get(0) );
for (int i = 1; i < indices.size(); i++) {
int index = indices.get(i);
if ( listToCheck.get(index) != val ) {
return false;
}
}
}
return true;
}
我会循环所有索引列表并检查另一个列表中相应索引的所有值。
测试
public static void main(String[] args) {
List<List<Integer>> indexList = new ArrayList<>();
indexList.add(Arrays.asList(0,1));
indexList.add(Arrays.asList(4,5,6));
System.out.println(indexList);
List<Integer> tempList = Arrays.asList(1,1,4,5,2,3,2);
List<Integer> tempList2 = Arrays.asList(3,3,4,5,6,6,6);
System.out.println(tempList);
System.out.println(checkIndex( indexList, tempList));
System.out.println(tempList2);
System.out.println(checkIndex( indexList, tempList2));
}
//输出
[[0, 1], [4, 5, 6]]
[1, 1, 4, 5, 2, 3, 2]
false
[3, 3, 4, 5, 6, 6, 6]
true