如何检查列表是否是Java中另一个列表的子集? [英] How to check if a list is a subset of another list in java?

查看:988
本文介绍了如何检查列表是否是Java中另一个列表的子集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查列表是否是java中另一个列表的子集。我使用了for循环来检查元素,并且我有一个名为same的变量,每次元素相同时都会增加。问题是仅当元素位于相同位置时列表才返回true



例如:

 (0,1)(0,1,2,3)是
(1,0)(0,1,2,3)否

我已经写了下面的代码:

  public Boolean contains(ItemsList ilist){
int same = 0;

if(empty()){
返回false;
} else {
ItemNode a = this.first;
ItemNode b = ilist.first;

表示(b = ilist.first; b!= null; b = b.next){
表示(a = this.first; a!= null; a = a.next ){
if(a.item == b.item){
same ++;
}
}
}
}

回报(相同> 0);
}


解决方案

解决此问题的方法与解决子串匹配问题非常相似。



首先检查是否假定子集列表的第一个元素(以下简称SSL)。



一旦找到匹配项,请注意索引(此后在找到匹配项的主列表中将其称为 myGuy ,请继续



如果匹配完成则返回,如果不匹配,则可以选择两者之一。主列表中剩余元素,然后递增 myGuy ,然后它成为您开始在主列表中进行迭代的新索引。

如果没有剩余元素并且仍然没有完全匹配,则它不是子集。



这是您在Java中的操作方法:

 私有静态布尔checkSubList(int [] mainList,int [] subList){
int currentIteratingPointer;
int matchCounter = 0;

for(int i = 0; i< mainList.length; i ++){
currentIteratingPointer = i;
for(int j = 0; j< subList.length; j ++){
if(mainList [cur rentIteratingPointer] == subList [j]){
System.out.println(mainList [currentIteratingPointer]);
++ matchCounter;
++ currentIteratingPointer;
} else {
--matchCounter;
休息时间;
}
}

i = currentIteratingPointer;
}

返回matchCounter == subList.length; //如果将
更改为int并返回matchCounter a
}

I'm trying to check if a list is a subset of another in java . I used the for loop to check the elements and i have a variable called same that is incremented each time the elements are the same . The problem is that the list returns true only if the elements are in identical positions

For example :

(0,1) (0,1,2,3 ) true
(1,0) (0,1,2,3) false 

I have written the code below :

public Boolean contains(ItemsList ilist) {
    int same = 0;

    if (empty()) {
        return false;
    } else {
        ItemNode a = this.first;
        ItemNode b = ilist.first;

        for (b = ilist.first; b != null; b = b.next) {
            for (a = this.first; a != null; a = a.next) {
                if (a.item == b.item) {
                    same++;
                }
            }
        }
    }

    return (same > 0);
}

解决方案

The approach to solve this problem is very similar to solving substring matching problem.

You start by checking if the first element of the supposedly subset list(henceforth to be refereed to as SSL).

Once you get a match, note the index (henceforth will be referred to as myGuy in the main list where the match is found, proceed to check if the subsequent elements of SSL matches the main list.

If your match is complete then simply return. If not, then you can do one of the two. If the main list has elements left, then increment myGuy and then it becomes the new index from where you start to iterate in the main list.
If no elements are left and still no complete match then it's not a subset.

Here is how you can do it in Java:

private static boolean checkSubList(int[] mainList, int[] subList) {
    int currentIteratingPointer;
    int matchCounter = 0;

    for (int i = 0; i < mainList.length; i++) {
        currentIteratingPointer = i;
        for (int j = 0; j < subList.length; j++) {
            if (mainList[currentIteratingPointer] == subList[j]) {
                System.out.println(mainList[currentIteratingPointer]);
                ++matchCounter;
                ++currentIteratingPointer;
            } else {
                --matchCounter;
                break;
            }
        }

        i = currentIteratingPointer;
    }

    return matchCounter == subList.length; // You can count the number of occurance of the sublist if you change
    // it to int and return the matchCounter a
}

这篇关于如何检查列表是否是Java中另一个列表的子集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆