有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java比较两个列表中的元素

我有两个包含整数的列表,如下所示:

def first = [10, 12, 3, 6, 9, 8, 33]
def second = [8, 9, 5, 6]

每个列表中的元素数量可以完全任意。我还有一个阈值,也是一个数字:

def threshold = 3 

我必须成对检查比较两个数组中的所有元素,并检查它们的差异是否为<;=门槛因此,我必须输出所有这些元素

因此,就我而言: 10, 12, 3, 6, 9, 8来自第一个列表,而8, 9, 5, 6来自第二个列表。因为

abs(10-8) <= 3
abs(12-9) <= 3
abs(3-5) <= 3
abs(6-6) <= 3

这里由于第一个列表比第二个列表包含更多的元素,我必须比较第一个列表元素和第二个列表中的最后一个元素

abs(9-6) <= 3
abs(8-6) <= 3
abs(33-6) >= 3, stop here!

Groovy和Java答案是合适的

另外,这是一个算法问题,并且已经有一些算法用于此目的了吗


共 (3) 个答案

  1. # 1 楼答案

    在下面的实现中,我使用了set数据结构来防止结果中的元素重复,如果要显示重复的元素,可以使用arraylist而不是set

    import java.util.*;
       public class CompareElements{
    
    
    
          public static void main(String[ ] arg){
    
    
            int [] firstList = {10, 12, 3, 6, 9, 8, 33};
            int [] secondList = {8, 9, 5, 6};
    
            int firstListLength = firstList.length;
            int secondListLength = secondList.length;
            // i have used set data structure to prevent duplication of elements in the result
            Set<Integer>result=new HashSet<Integer>();
    
            // iterate over the two list and get the absolute value for each two corresponding elements
            // and check if the difference is <= 3 , the two elements are added to the result 
            for(int i=0;i<Math.min(firstList.length, secondList.length);i++) {
                if(Math.abs(firstList[i]-secondList[i]) <= 3)
                {
                    result.add(firstList[i]);
                    result.add(secondList[i]);
                }
            }
    
            // here we are trying to handle the case when the lists have different lengths
            // and the second list length is greater 
            if(firstListLength < secondListLength)
            {
                for(int i =firstListLength-1;i<secondListLength;i++)
                {
                    if(Math.abs(firstList[firstListLength-1]-secondList[i]) <= 3)
                    {
                        result.add(firstList[firstListLength-1]);
                        result.add(secondList[i]);
                    }
                }
            }
            // here we are trying to handle the case when the lists have different lengths
            // and the first list length is greater 
            else if (firstListLength > secondListLength)
            {
                for(int i =secondListLength-1;i<firstListLength;i++)
                {
                    if(Math.abs(firstList[i]-secondList[secondListLength-1]) <= 3)
                    {
                        result.add(firstList[i]);
                        result.add(secondList[secondListLength-1]);
                    }
                }
            }
            System.out.println(result.toString());
    
    
    
    
    
           }
    
      }
    
  2. # 2 楼答案

    如果您不需要可怕的最终重复元素限制,那么在groovy中:

    [first,second].transpose()
                  .every { Math.abs(it[0]-it[1]) < threshold }
    

    编辑

    给定此函数,将列表填充为特定宽度(默认为列表的最大长度):

    def paddedPairs(List lists, Integer width=lists*.size().max()) {
        (0..<width).collect { p -> lists.collect { it[ p >= it.size() ? -1 : p] } }
    }
    

    你可以做:

    paddedPairs([first, second]).every { Math.abs(it[0]-it[1]) < threshold }
    
  3. # 3 楼答案

    这是一种java方式来完成它。既然你知道减法,那谁先来并不重要。因此,让我们利用这一点,找出哪一个更长。一旦我们知道了这一点,我们就知道该强制哪一个重复它的最后一个元素

    if (first.length() > second.length()) {
        longer = first;
        shorter = second;
    } else {
        longer = second;
        shorter = first;
    }
    
    last = shorter.length() - 1;
    
    for(int i = 0; i < longer.length(); i++) {
        s = shorter.get(Math.min(last, i));
        l = longer.get(i);
        if (abs(l-s) > threshold) {
            break;
        }
    }