繁体   English   中英

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

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

我正在尝试创建一个函数来获取不在另一个列表中的列表中的下一项,同时在到达末尾时循环到列表的开头。

基本上我想要发生的是:

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

我试过这样做:

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];
}

但这不起作用,索引保持不变。

有没有更好的方法来做到这一点/类似于我不知道的事情?

一些变化,它就像一个魅力:

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];
    }
}

要点是该index必须是全局的。

取决于声明listlist2 的位置。 我建议提供字符串数组(列表)和列表接口(列表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;
}

上面的示例nextItem()方法返回一个整数值,该值将是字符串数组(列表)中的项目不包含在列表接口集合(列表2)中的索引值。 您对这个方法的自然第一次调用将提供一个0作为startIndex参数的参数。 要获取listlist2 中共享的下一项,您需要提供从前一个nextItem()调用返回的索引值,添加1 ,并将其作为startIndex值提供给下一个nextItem()方法调用。 下面是一个例子:

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++;
    } 
}

控制台窗口将显示:

banana
egg
grape  

以下示例代码不使用循环,在此处提供可能是对代码正在执行的操作提供更好的视觉帮助:

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
}

控制台窗口将显示:

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

关键是不断增加index直到在list2找到list的元素。 我在下面给出的程序中使用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++];
    }
}

输出:

egg
grape
banana
egg
grape

如有任何问题/疑问,请随时发表评论。

暂无
暂无

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

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