打印空括号?

时间:2015-09-12 00:56:50

标签: java arrays list

我有一个简单的程序,应该采用两个数组,将它们转换为列表,并取出相同的值。出于某种原因,虽然我得到的值是:

[]
[]
[]
[]

虽然结果应该是" egg"," lasers"," hats"," pie" - "激光","帽子"哪个应该让我得到#34; egg" "馅饼"

这是我的代码:

public class Arraystring {

    public static void main(String[] args) {
        String[] things = { "eggs", "lasers", "hats", "pie" };
        List<String> list1 = new ArrayList<String>();
        for (String x : things) { 
            list1.add(x);
        }
        String[] thingstwo = { "lasers", "hats" };
        List<String> list2 = new ArrayList<String>();
        for (int i = 0; i < list2.size(); i++) {
            list2.add(thingstwo[i]);
        }
        editlist(list1, list2);
    }

    public static void editlist(Collection<String> l1, Collection<String> l2) {

        Iterator<String> it = l1.iterator();
        while (it.hasNext()) {
            if (l2.contains(it.next())) {
                System.out.println("hui");
                it.remove();
            }
            System.out.println(l2);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先是你在Arraystring {..}

中的行
    for (int i = 0; i < list2.size(); i++) {

应该是

    for (int i = 0; i < thingstwo.length; i++) {

然后你在editlist(..)中打印错误的列表:

        System.out.println(l2);

应该是

        System.out.println(l1);