1

How can I alter the below method to work with an ArrayList? I was thinking something like this:

public static boolean sortArrayList(ArrayList<Integer> list) {
    return false;
}

but i'm not sure how to complete it.

Here is the method that I am trying to convert from working with an Array to instead work with an ArrayList:

public static boolean sortArrayList(final int[] data) {
    for(int i = 1; i < data.length; i++) {
        if(data[i-1] > data[i]) {
            return false;
        }
    }
    return true;
}
2
  • 1
    Since your sortArrayList method does not sort the ArrayList, you may want to rename it to something like isASortedArrayList Commented Apr 1, 2016 at 1:04
  • Your method name! It could literally not be any more misleading. Commented Apr 1, 2016 at 1:49

3 Answers 3

1
public static boolean sortArrayList(final ArrayList <Integer> data) {
    for (int i = 1; i < data.size(); i++) {
        if (data.get(i - 1) > data.get(i)) {
            return false;
        }
    }
    return true;
}
1

I have a few problems with the accepted answer, as given by @Sanj: (A) it doesn't handle nulls within the list, (B) it is unnecessarily specialized to ArrayList<Integer> when it could easily be merely Iterable<Integer>, and (C) the method name is misleading.

NOTE: For (A), it's quite possible that getting an NPE is appropriate - the OP didn't say. For the demo code, I assume that nulls are ignorable. Other interpretations a also fair, e.g. null is always a "least" value (requiring different coding, LAAEFTR). Regardless, the behaviour should be JavaDoc'ed - which I didn't do in my demo #8>P

NOTE: For (B), keeping the specialized version might improve runtime performance, since the method "knows" that the backing data is in an array and the compiler might extract some runtime efficiency over the version using an Iterable but such claim seem dubious to me and, in any event, I would want to see benchmark results to support such. ALSO Even the version I demo could be further abstracted using a generic element type (vs limited to Integer). Such a method might have definition like:

public static <T extends Comparable<T>> boolean isAscendingOrder(final Iterable<T> sequence)

NOTE: For (C), I follow @Valentine's method naming advice (almost). I like the idea so much, I took it one step further to explicitly call out the directionality of the checked-for-sortedness.

Below is a demonstration class that shows good behaviour for a isAscendingOrder which address all those issues, followed by similar behaviour by @Sanj's solution (until the NPE). When I run it, I get console output:

true, true, true, true, false, true
------------------------------------
true, true, true, true, false, 
Exception in thread "main" java.lang.NullPointerException
    at SortCheck.sortArrayList(SortCheck.java:35)
    at SortCheck.main(SortCheck.java:78)

.

import java.util.ArrayList;

public class SortCheck
{
    public static boolean isAscendingOrder(final Iterable<Integer> sequence)
    {
        Integer prev = null;
        for (final Integer scan : sequence)
        {
            if (prev == null)
            {
                prev = scan;
            }
            else
            {
                if (scan != null)
                {
                    if (prev.compareTo(scan) > 0)
                    {
                        return false;
                    }

                    prev = scan;
                }
            }
        }

        return true;
    }

    public static boolean sortArrayList(final ArrayList<Integer> data)
    {
        for (int i = 1; i < data.size(); i++)
        {
            if (data.get(i - 1) > data.get(i))
            {
                return false;
            }
        }
        return true;
    }

    private static ArrayList<Integer> createArrayList(final Integer... vals)
    {
        final ArrayList<Integer> rval = new ArrayList<>();

        for(final Integer x : vals)
        {
            rval.add(x);
        }

        return rval;
    }

    public static void main(final String[] args)
    {
        final ArrayList<Integer> listEmpty     = createArrayList();
        final ArrayList<Integer> listSingleton = createArrayList(2);
        final ArrayList<Integer> listAscending = createArrayList(2, 5, 8, 10  );
        final ArrayList<Integer> listPlatuea   = createArrayList(2, 5, 5, 10  );
        final ArrayList<Integer> listMixedUp   = createArrayList(2, 5, 3, 10  );
        final ArrayList<Integer> listWithNull  = createArrayList(2, 5, 8, null);

        System.out.print(isAscendingOrder(listEmpty    ) + ", ");
        System.out.print(isAscendingOrder(listSingleton) + ", ");
        System.out.print(isAscendingOrder(listAscending) + ", ");
        System.out.print(isAscendingOrder(listPlatuea  ) + ", ");
        System.out.print(isAscendingOrder(listMixedUp  ) + ", ");
        System.out.print(isAscendingOrder(listWithNull ) + "\n");

        System.out.println("------------------------------------");

        System.out.print(sortArrayList(listEmpty    ) + ", ");
        System.out.print(sortArrayList(listSingleton) + ", ");
        System.out.print(sortArrayList(listAscending) + ", ");
        System.out.print(sortArrayList(listPlatuea  ) + ", ");
        System.out.print(sortArrayList(listMixedUp  ) + ", ");
        System.out.print(sortArrayList(listWithNull ) + "\n");
    }
}
0

Try below function, it takes integer array and converts it into a ArrayList and then computes the result :

public static boolean sortArrayList(final int[] data) {
        List<Integer> aList = new ArrayList<Integer>();
        for (int index = 0; index < data.length; index++)
            aList.add(data[index]);

        for (int i = 1; i < aList.size(); i++) {
            if (aList.get(i - 1) > aList.get(i)) {
                return false;
            }
        }
        return true;
    }

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.