1

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.

Ihor Patsian
  • 1,288
  • 2
  • 15
  • 25
  • 3
    Possible duplicate of [comparing elements of two arrayList in java](http://stackoverflow.com/questions/20176087/comparing-elements-of-two-arraylist-in-java) – Julien Lopez Nov 14 '16 at 12:04
  • How do you plan to compare the two lists of cards? In order, or something else? – Tim Biegeleisen Nov 14 '16 at 12:05
  • I would like to compare each element individually Tim, then product an output and move onto the next element (if that is possible). – I just want to learn. Nov 14 '16 at 12:07
  • Julien, my question differs as my arraylists consist of random numbers and I want to access individual elements in my array lists and compare them, producing a print statement when one is larger than the other. – I just want to learn. Nov 14 '16 at 12:19

2 Answers2

5

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.

ANIL VISHVKARMA
  • 363
  • 1
  • 15
0

Assuming you have 2 ArrayLists 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

Roel Strolenberg
  • 2,922
  • 1
  • 15
  • 29
  • The two size 10 array lists contain random numbers, I want to compare them and product a if statement, with a print statement if the first array list element is larger than the second arraylist element and vice versa. – I just want to learn. Nov 14 '16 at 12:16
  • Updated my answer, is this more to your liking? This only works if the lists contain integers or doubles/floats etc – Roel Strolenberg Nov 14 '16 at 12:20
  • Yes thank you Roel, the randoms are integers from 0-10. How would I be able to utilise your code, so that I could do that to each of the 10 elements in the arraylists? – I just want to learn. Nov 14 '16 at 12:25
  • Oh ok thank you, when I tried to integrate your code with my program I got a few errors such as: illegal start of type for the 'for' and identifier expected for the rest? – I just want to learn. Nov 14 '16 at 12:38
  • If you have problems integrating this, maybe your java basics is too low and maybe follow a tutorial. The code is very straightforward for any beginning java coder – Roel Strolenberg Nov 14 '16 at 12:45