简体   繁体   English

确定 arraylist2 的输入是否存在于 arraylist1 中,因为它们被放入

[英]Determining if inputs of arraylist2 exist in arraylist1 AS they're being put in

I'm making a program where an arrayList (list1) is made beforehand, and then as the values for list2 are being entered, I want to see if that value already exists in list1.我正在制作一个程序,其中预先制作了 arrayList (list1),然后在输入 list2 的值时,我想查看 list1 中是否已经存在该值。

Here's what I'm trying to do: Just for practice, I want to make a program for truck day at work.这就是我想要做的:只是为了练习,我想为工作中的卡车日制作一个程序。 Before the truck arrives, a list of what items can fit on shelves (list1) is made.在卡车到达之前,会列出可以放在货架上的物品清单 (list1)。 As each box is unloaded off the truck, the code for it is scanned and added to list2, and if that code appears on list1, I want it to print out "Out to shelf."当每个箱子都从卡车上卸下时,它的代码被扫描并添加到 list2 中,如果该代码出现在 list1 上,我希望它打印出“Out to Shelf”。 or something similar.或类似的东西。 If the incoming boxes code does not appear on list1, do nothing and continue accepting values to add on to list2.如果传入框代码未出现在 list1 上,则不执行任何操作并继续接受要添加到 list2 的值。 However, I can only figure out how to compare the lists AFTER they've been made, but it is important to compare them AS list2 is being created.但是,我只能弄清楚如何在创建列表之后比较它们,但是在创建 list2 时比较它们很重要。


//get bin list codes
        System.out.println("Enter bin list codes, type in 0 when finished!");
        int binCode = input.nextInt();
        while (binCode != 0) {
            binList.add(binCode);
            binCode = input.nextInt();
        }

//get truck list codes
        System.out.println("Enter truck list codes, type in 0 when finished!");
        int truckCode = input.nextInt();
        while (truckCode != 0) {
            truckList.add(truckCode);
            truckCode = input.nextInt();
        }

//print out bin list
        for(int i : binList) {
            System.out.println(i);
        }
        System.out.println("--------");

//print out truck list
        for(int i : truckList) {
            System.out.println(i);
        }
        System.out.println("--------");
    }

Try this perhaps:试试这个:

//get truck list codes
System.out.println("Enter truck list codes, type in 0 when finished!");
int truckCode = input.nextInt();
while (truckCode != 0) {
    if(binList.contains(truckCode)) {
        //do your print out here
    }
    truckList.add(truckCode);
    truckCode = input.nextInt();
}

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

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