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.

4
  • 3
    Possible duplicate of comparing elements of two arrayList in java Commented Nov 14, 2016 at 12:04
  • How do you plan to compare the two lists of cards? In order, or something else? Commented Nov 14, 2016 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). Commented Nov 14, 2016 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. Commented Nov 14, 2016 at 12:19

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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

5 Comments

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.
Updated my answer, is this more to your liking? This only works if the lists contain integers or doubles/floats etc
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?
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?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.