Results 1 to 5 of 5
- 04-13-2011, 04:23 AM #1
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
Checking if an ArrayList is sorted
Hi all,
I need a method which checks to see if an ArrayList is sorted in a ascending order. I keep getting an IndexOutofBound error. Wonder if you can help me to work around this.
Java Code:public static boolean isSorted(ArrayList<Double> array) { boolean result = true; for(int i = 0; i <array.size(); i++) { if(array.get(i) >= array.get(i+1)) { result = false; break; } } return result; }
- 04-13-2011, 04:26 AM #2
What happens when you get to the last element in the List? You still check to see if it is before the "next" element when there isn't one.
- 04-13-2011, 04:32 AM #3
Member
- Join Date
- Apr 2011
- Posts
- 2
- Rep Power
- 0
- 04-13-2011, 04:34 AM #4
Simple, stop looping too many times.
- 04-13-2011, 06:27 AM #5
Member
- Join Date
- Feb 2011
- Posts
- 19
- Rep Power
- 0
Similar Threads
-
Binary-algorithm -> Insert String to sorted String-ArrayList
By Muskar in forum Advanced JavaReplies: 12Last Post: 11-26-2010, 08:33 AM -
Sorted Doubly linked List
By student2889 in forum New To JavaReplies: 1Last Post: 10-14-2010, 10:05 AM -
Casting an int from a sorted set
By Bmack in forum New To JavaReplies: 2Last Post: 03-17-2010, 07:09 PM -
Sorted LinkList problem
By koolaqua16 in forum Advanced JavaReplies: 1Last Post: 08-08-2009, 06:49 AM -
My doublyLinked list does not get sorted
By hasani6leap in forum New To JavaReplies: 0Last Post: 01-06-2008, 03:09 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks