2

I currently have a String Array

String[] properSequence = ability.getSequence();

And I want to compare it to an ArrayList

ArrayList<String> sequence

As of right now I'm doing,

boolean matchesSequence = true;

String[] properSequence = ability.getSequence();

int index = 0;
for(String s : sequence) {
   String s1 = properSequence[index];
   if(!s1.equalsIgnoreCase(s)) {
      matchesSequence = false;
      break;
   }
   index++;
}

if(matchesSequence) {
   // Matches, code
}

I was wondering if there's an easier/prettier way of doing this, seems a bit redundant.

2
  • In this case, using a foreach-loop makes things less legible. Use a classic index-based loop, and then do if(!properSequence[i].toLowerCase().equals(sequence.get(i).toLowerCase())) return false or something along those lines. Even better, use Apache StringUtils, which will take care of null safety for ya. Commented Aug 9, 2020 at 19:22
  • Also, I would definitely encapsulate the list comparison into its own method - something along the lines of private boolean compareList(Iterator a, Iterator b). Commented Aug 9, 2020 at 19:24

4 Answers 4

1

Your code may throw ArrayIndexOutOfBoundsException. Check the bounds of array as well as the list as shown below:

for(int i = 0; i < Math.min(sequence.size(), properSequence.length); i++) {
   if(!sequence.get(i).equalsIgnoreCase(properSequence[i])) {
      matchesSequence = false;
      break;
   }
}

Alternatively,

for(int i = 0; i < sequence.size() && i < properSequence.length; i++) {
   if(!sequence.get(i).equalsIgnoreCase(properSequence[i])) {
      matchesSequence = false;
      break;
   }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can use built-in equals method:

if(!Arrays.equals(sequence.toArray(),properSequence))
    matchesSequence = false;

1 Comment

This will violate what OP wants to achieve. OP wants to compare in a case-insensitive way.
1

You could convert the String[] into an ArrayList and compare those two, but you can't do that if you want equalsIgnoreCase() in the comparison. However, if you put all the elements in the String[] and the ArrayList in upper (or lower) case, you could do this:

if (Arrays.asList(properSequence).equals(sequence)) 
{
   ...
}

Comments

1

Arrays and Lists are only considered equal to each other if the elements are equal and in the same order.

To compare an Array of object to a List of the same object you can do the following using the Arrays#compare method. Different versions have different capabilities. This example uses Strings and does a case insensitive compare. It also makes use of List.toArray introduced in JDK 11

List<String> list = List.of("A", "B", "C");
String[] array1 = { "a", "b", "c" };
String[] array2 = { "a", "c", "b" };

for (String[] array : new String[][] { array1, array2 }) {
    
    if (Arrays.compare(array, list.toArray(String[]::new),
            String.CASE_INSENSITIVE_ORDER) == 0) {
        System.out.printf("%s equals %s%n",
                Arrays.toString(array), list);
    } else {
        System.out.printf("%s does not equal %s%n",
                Arrays.toString(array), list);
    }
}

Prints

[a, b, c] equals [A, B, C]
[a, c, b] does not equal [A, B, C]

3 Comments

so Arrays.compare does work with an array of arrays and internally automatically iterates over each array and compares it to the list which is cast to an array?
Almost. But not an array of arrays. It does iterate over the arrays and does a 1 to 1 compare. But the List is not cast to an array but the toArray method returns an array of the type specified. That method was introduced in JDK 11. The array of arrays I am using is just to specify separate single D arrays for demonstration. Each of those test arrays are compared to the List.
To compare an array of arrays to another of the same, something like a DeepCompare would need to exist. But the Arrays class does not provide that.

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.