Thursday, April 19, 2012

Java: Comparing Arrays

Want to compare arrays of which item may not be in same order. Here is the solution:

public static boolean compareArrays(String[] actual, String[] expected)
    {
        if(actual == null || expected == null)
            return false;
       
        Arrays.sort(actual);
        Arrays.sort(expected);
       
        if(Arrays.equals(actual, expected))
            return true;
        else
            return false;
    }

No comments: