【问题标题】:Determining if inputs of arraylist2 exist in arraylist1 AS they're being put in确定 arraylist2 的输入是否存在于 arraylist1 中,因为它们被放入
【发布时间】:2019-10-08 02:11:01
【问题描述】:

我正在制作一个预先创建 arrayList (list1) 的程序,然后在输入 list2 的值时,我想查看 list1 中是否已经存在该值。

这就是我想要做的事情: 只是为了练习,我想为工作中的卡车日制作一个程序。在卡车到达之前,会列出可以放在货架上的物品清单 (list1)。当每个箱子都从卡车上卸下时,它的代码被扫描并添加到 list2 中,如果该代码出现在 list1 上,我希望它打印出“Out to Shelf”。或类似的东西。如果传入框代码未出现在 list1 上,则不执行任何操作并继续接受要添加到 list2 的值。 但是,我只能弄清楚如何在创建列表之后比较它们,但是在创建 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("--------");
    }

【问题讨论】:

  • 您的代码没有list1list2
  • 请阅读minimal reproducible example 并相应地增强您的问题。不知道您究竟在寻找什么。

标签: java arraylist compare


【解决方案1】:

不妨试试这个:

//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();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-08-09
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    • 1970-01-01
    • 2016-01-22
    • 2017-01-03
    • 2015-08-27
    相关资源
    最近更新 更多