[英]How to compare elements in an array list using the index provided by another arraylist
I have an ArrayList: A
containing array
of indexes something like this [[0,1],[4,5,6]]
.我有一个
ArrayList: A
包含索引array
,类似于[[0,1],[4,5,6]]
。 This list is dynamic and may grow in size based on some prior operations that I do I have 2 more integer arraylists
of equal sizes say 7
.这份名单是动态的,基于我有2周整数一些前期操作,我做的规模可能会增长
arraylists
大小相等的说7
。 something like this: B: [1,1,4,5,2,3,2]
and C: [3,3,4,5,6,6,6]
像这样:
B: [1,1,4,5,2,3,2]
和C: [3,3,4,5,6,6,6]
I need help to compare the elements at indexes represented by arrayList: A
in B
and C
seperately我需要帮助来比较由
arrayList: A
in B
和C
表示的索引处的元素
Example:例子:
compare elements in arrayList B at index [0,1]
and check if list.get[0] == list.get[1]
and for [4,5,6]
check if list.get[4] == list.get[5] == list.get[6]
比较 arrayList B 中索引
[0,1]
处的元素并检查list.get[0] == list.get[1]
和[4,5,6]
检查是否list.get[4] == list.get[5] == list.get[6]
same for array list C
array list C
相同
Any help much appreciated.非常感谢任何帮助。 Thanks in advance
提前致谢
Just as you would say it, use the values of arraylist A
as indexes for the other lists:正如您所说,使用 arraylist
A
的值作为其他列表的索引:
Example A = [0,1] , B[3, 3]示例A = [0,1] , B[3, 3]
You need to check if:您需要检查是否:
B[0] == B[1]
but rewriting the indexes as values of A
you get:但是将索引重写为
A
值,您会得到:
B[A[0]] == B[A[1]]
Just extend this to the general case and you have solved the problem.只需将其扩展到一般情况即可解决问题。
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;
}
I would loop all index list and check for all values of the corresponding index in the other list.我会循环所有索引列表并检查另一个列表中相应索引的所有值。
Test测试
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));
}
//Output //输出
[[0, 1], [4, 5, 6]]
[1, 1, 4, 5, 2, 3, 2]
false
[3, 3, 4, 5, 6, 6, 6]
true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.