简体   繁体   English

获取列表中不在 Java 中另一个列表中的下一项

[英]Get next item in list that is not in another list in Java

I'm trying to make a function to get the next item in a list that is not in another list, while also looping to the beginning of the list when it reaches the end.我正在尝试创建一个函数来获取不在另一个列表中的列表中的下一项,同时在到达末尾时循环到列表的开头。

Basically what I want to happen is:基本上我想要发生的是:

String[] list = new String[]{"apple", "banana", "cherry", "donut", "egg", "fish", "grape"};
List<String> list2 = new ArrayList<String>( Arrays.asList("apple", "cherry", "donut", "fish"));
int index = 4;
System.out.println(nextItem());
System.out.println(nextItem());
System.out.println(nextItem());
System.out.println(nextItem());
System.out.println(nextItem());
//Returns:
//egg
//grape
//banana
//egg
//grape

I have tried doing this:我试过这样做:

public String nextItem() {
    index++;
    if(index == list.size()) index = 0;
    while(list2.contains(list[index])) {
        index++;
        if(index == list.size()) index = 0;
    }
    return list[index];
}

but that does not work, the index just stays the same.但这不起作用,索引保持不变。

Is there a better way to do this/something similar to this that I don't know of?有没有更好的方法来做到这一点/类似于我不知道的事情?

A few changes and it works like a charm:一些变化,它就像一个魅力:

public class Example {
    String[] list = new String[]{"apple", "banana", "cherry", "donut", "egg", "fish", "grape"};
    List<String> list2 = new ArrayList<>(Arrays.asList("apple", "cherry", "donut", "fish"));
    public int index = 4;

    public static void main(String[] args) {
        new Example().start();
    }

    public void start() {
        for (int i = 0; i < 100000; i++) {
            System.out.println(nextItem());
        }
    }

    public String nextItem() {
        index++;
        if (index == list.length) index = 0;
        while (list2.contains(list[index])) {
            index++;
            if (index == list.length) index = 0;
        }
        return list[index];
    }
}

The main point being that index must be global.要点是该index必须是全局的。

Depends where list and list2 are declared.取决于声明listlist2 的位置。 I would suggest supplying the String Array (list) and the List Interface (list2) as arguments to the nextItem() method as well as a Starting Index value as a third argument, for example:我建议提供字符串数组(列表)和列表接口(列表2)作为nextItem()方法的参数,以及作为第三个参数的起始索引值,例如:

public static int nextItem(String[] stringArray, List<String> listInterface, int startIndex) {
    int index = -1;
    for (int i = startIndex; i < stringArray.length; i++) {
        if (!listInterface.contains(stringArray[i])) {
            return i;
        } 
    }
    return -1;
}

The above example nextItem() method returns an Integer value which would be the index value at which an item within the String Array (list) is not also contained within the List Interface collection (list2).上面的示例nextItem()方法返回一个整数值,该值将是字符串数组(列表)中的项目不包含在列表接口集合(列表2)中的索引值。 Your natural first call to this method would supply a 0 as the argument for the startIndex parameter.您对这个方法的自然第一次调用将提供一个0作为startIndex参数的参数。 To get the next item which is not shared in both list and list2 , you would supply the the returned index value from the previous nextItem() call, add 1 , and provide it as the startIndex value to the next nextItem() method call.要获取listlist2 中共享的下一项,您需要提供从前一个nextItem()调用返回的索引值,添加1 ,并将其作为startIndex值提供给下一个nextItem()方法调用。 Here's an example:下面是一个例子:

String[] list = {"apple", "banana", "cherry", "donut", "egg", "fish", "grape"};
List<String> list2 = new ArrayList<>( Arrays.asList("apple", "cherry", "donut", "fish"));

int idx = 0;
while (idx != -1) {
    idx = nextItem(list, list2, idx);
    if (idx != -1) {
        System.out.println(list[idx]);
        idx++;
    } 
}

The Console Window would display:控制台窗口将显示:

banana
egg
grape  

The following example code doesn't use a loop and is supplied here to perhaps be a better visual aid to what the code is doing:以下示例代码不使用循环,在此处提供可能是对代码正在执行的操作提供更好的视觉帮助:

String[] list = {"apple", "banana", "cherry", "donut", "egg", "fish", "grape"};
List<String> list2 = new ArrayList<>( Arrays.asList("apple", "cherry", "donut", "fish"));

String noMore = "** There are no more non-shared items in the lists! **";

// Get first non-shared item
int idx = nextItem(list, list2, 0);
if (idx > -1) {
    System.out.println(list[idx]);
}
else {
    System.out.println("Both lists contain all the same items");
    // return from method or event
}

// Get second non-shared item
idx = nextItem(list, list2, idx + 1);
if (idx > -1) {
    System.out.println(list[idx]);
}
else {
    System.out.println(noMore);
    // return from method or event
}

// Get third non-shared item
idx = nextItem(list, list2, idx + 1);
if (idx > -1) {
    System.out.println(list[idx]);
}
else {
    System.out.println(noMore);
    // return from method or event
}

// Get fourth non-shared item (if any)
idx = nextItem(list, list2, idx + 1);
if (idx > -1) {
    System.out.println(list[idx]);
}
else {
    System.out.println(noMore);
    // return from method or event
}

The console Window would display:控制台窗口将显示:

banana
egg
grape
** There are no more non-shared items in the lists! **

The key is to keep increasing index till an element from list is found in list2 .关键是不断增加index直到在list2找到list的元素。 I have done it using the while loop in the program given below:我在下面给出的程序中使用while循环完成了它:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    static String[] list = new String[] { "apple", "banana", "cherry", "donut", "egg", "fish", "grape" };
    static List<String> list2 = new ArrayList<String>(Arrays.asList("apple", "cherry", "donut", "fish"));
    static int index = 4;

    public static void main(String[] args) {
        System.out.println(nextItem());
        System.out.println(nextItem());
        System.out.println(nextItem());
        System.out.println(nextItem());
        System.out.println(nextItem());
    }

    static String nextItem() {
        if (index == list.length) {
            index = 0;
        }
        while (list2.contains(list[index])) {
            index++;
        }
        return list[index == list.length ? 0 : index++];
    }
}

Output:输出:

egg
grape
banana
egg
grape

Feel free to comment in case of any issue/doubt.如有任何问题/疑问,请随时发表评论。

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

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