Reputation: 55
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. 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.
Upvotes: 1
Views: 1756
Reputation: 363
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 -
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. If value of list1 and list2 are equal it will return true else false.
Upvotes: 5
Reputation: 2950
Assuming you have 2 ArrayList
s both of size 10, you can loop through them and compare each item individually:
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
Upvotes: 0