简体   繁体   English

比较ArrayList的Array中的元素-Java

[英]Compare elements in Array of ArrayList - Java

I am using the following code to traverse through a array of arraylist: 我正在使用以下代码遍历arraylist数组:

for(List<Long> innerList : arr) {
    for(Long number : innerList) {
       System.out.println(number);
    }
}

This code returns the number in each array of the arraylist.I want to compare the first element of the first array of the arraylist with all other elements of all other array of the arraylist. 这段代码返回arraylist的每个数组中的数字。我想将arraylist的第一个数组的第一个元素与arraylist的所有其他数组的所有其他元素进行比较。 How to do this? 这个怎么做?

Complete program: 完整程序:

static long getWays(long sum, long[] changes) {
    int count=0;
    ArrayList<ArrayList<Long>> arr = new ArrayList<ArrayList<Long>>();
    for(int i=0;i<changes.length;i++){
        ArrayList<Long> arr1 = new ArrayList<Long>();
        long change = changes[i];
        long value = changes[i];
        arr1.add(change);
        for(int j=0;j<sum;j++){
            change = change + value;
            if(change>sum){
                break;
            }
            arr1.add(change);
        }
        arr.add(arr1);
    }
    for(List<Long> innerList : arr) {
        for(Long number : innerList) {
            System.out.println(number);
        }
    }
    return count;
}
Long first = arr.get(0).get(0);
for (List<Long> innerList: arr) {
  for (Long number: innerList) {
    System.out.println(number);
    // Here compare first and number
  }
}

You can keep the first element in a var. 您可以将第一个元素保留在var中。

I would go for a Stream.flatMap usage. 我会去使用Stream.flatMap

Returns a stream consisting of the results of replacing each element of this stream with the contents of a mapped stream produced by applying the provided mapping function to each element. 返回一个流,该流包括将流中的每个元素替换为通过将提供的映射函数应用于每个元素而生成的映射流的内容而得到的结果。

The flatMap() operation has the effect of applying a one-to-many transformation to the elements of the stream, and then flattening the resulting elements into a new stream. flatMap()操作的作用是对流的元素进行一对多转换,然后将生成的元素展平为新的流。

data.stream() //stream a list of list
    .flatMap( List::stream ) //stream each list
    // At this point, we have a Stream reading every `Long` values in those Collections
    .filter(l -> l.equals(value)) //filter only the equivalent value
    .count() - 1; //-1 to remove the first one

This would give you the number of occurence of the first value where : 这将为您提供第一个值的出现次数,其中:

final Long value = data.get(0).get(0);

Example : 范例:

List<List<Long>> data = new ArrayList<List<Long>>();

for(int i = 0; i < 5; ++i){
    data.add(new ArrayList<>());
    for(int j = 0; j < 10; ++j){
        data.get(i).add(1L * i * j);
    }
}

final Long value = data.get(0).get(0);
long count = data.stream()
                 .flatMap( List::stream )
                 .filter(l -> l.equals(value))
                 .count() - 1;
System.out.println(count);

13 13

You were not clear about the comparison you where doing so I let you adapt the filter . 您不清楚自己在进行的比较,所以我让您调整filter I simply count the occurrence of the first value here. 我只是在这里计算第一个值的出现。

Note that you still have some validation to prevent exception (check if the list aren't null, if there is at list one List with one Long ... 请注意,您仍然可以进行一些验证来防止异常(检查列表是否不为null,如果List有一个Long为1的List ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM