简体   繁体   English

比较两个不同数组列表的单个元素?

[英]Comparing individual elements of two different arraylists?

I have created a program with two arraylists ( packOfCardsOne and packOfCardsTwo ) each of which has ten random values stored, I know how to compare the two arraylists but I want to be able to compare individual elements.我创建了一个带有两个packOfCardsOne packOfCardsTwopackOfCardsOnepackOfCardsTwo )的程序,每个packOfCardsOne packOfCardsTwo都存储了十个随机值,我知道如何比较两个数组列表,但我希望能够比较单个元素。 I as thinking something like this below but I'm unable to get it to work:我在想下面这样的事情,但我无法让它工作:

  if (packOfCardsOne > packOfCardsTwo) {

      packofCardsOne.get(0);
      packOfCardsTwo.get(0); 
  }

Once compared as part of the if statement I'd then like to have a print statement with some output.一旦作为 if 语句的一部分进行比较,我就会想要一个带有一些输出的打印语句。

Assuming you are having 2 ArrayList list1 and list2 with size 10. You can simply iterate one list parallel to second list by comparing the values stored in both the list as shown below -假设您有 2 个 ArrayList list1 和 list2,大小为 10。您可以通过比较存储在两个列表中的值,简单地迭代一个与第二个列表平行的列表,如下所示 -

 List<String> list1 = new ArrayList<String>();
 list1.add("first");
 list1.add("second");
 list1.add("third");
 List<String> list2 = new ArrayList<String>();
 list2.add("first");
 list2.add("second");
 list2.add("third1");
 for (int i = 0; i<list2.size(); i++)
 {
     System.out.println(list1.contains(list2.get(i)));
 }

It will iterate the elements of list1 and compare the value with list2 elements.它将迭代 list1 的元素并将值与 list2 元素进行比较。 If value of list1 and list2 are equal it will return true else false.如果 list1 和 list2 的值相等,则返回 true 否则返回 false。

Assuming you have 2 ArrayList s both of size 10, you can loop through them and compare each item individually:假设您有 2 个ArrayList的大小均为 10,您可以遍历它们并单独比较每个项目:

for (int index = 0; index < 10; index++){
    if (packOfCardsOne.get(index) > packOfCardsTwo.get(index)){
        System.out.println("First pack's card is higher...")
    } else {
        System.out.println("Second pack's card is higher...")
    }
}

You can replace the .equals check with whatever you want as I'm unsure what's in the lists您可以将.equals检查替换为您想要的任何内容,因为我不确定列表中的内容

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

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