简体   繁体   English

如何使用另一个数组列表提供的索引比较数组列表中的元素

[英]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 BC表示的索引处的元素

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.

相关问题 如何使用 Java 8 的 Stream API 将 ArrayList 的元素与另一个 ArrayList 的每个元素进行比较? - How to compare the elements of an ArrayList with every element of another ArrayList using Stream API of Java 8? 将数组列表的元素复制到另一个数组列表,从而使用Java消除了第二个数组列表中的冗余 - copying elements of an arraylist to another array list eliminating redundancy in the 2nd arraylist using Java 在不使用Arraylist的情况下增加数组索引或将元素复制到另一个数组 - Increasing Array Index without using Arraylist or Copying Elements over to another Array 比较ArrayList的Array中的元素-Java - Compare elements in Array of ArrayList - Java 如何在Java中将数组元素与另一个数组进行比较 - how to compare elements of array with another array in java 如何将数组的元素与 ArrayList 中的另一个数组的元素进行比较? - How To Compare An Element of Array With Element of another array in a ArrayList? 如何流利地比较ArrayList元素? - How to compare ArrayList elements fluently? 如何比较 arraylist 中的 int[] 元素? - How compare int[] elements in arraylist? 如何基于另一个对象的列表比较和重新排列对象的ArrayList? - How to compare and reorder an ArrayList of objects based on a list of another objects? 如何比较一个ArrayList中的element(char)位置比Java中另一个ArrayList中其左侧元素的位置大 - How to compare an element(char) position in one ArrayList is bigger position than its left elements in another ArrayList in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM