Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Bubble sort in ArrayList

807601Jan 21 2008 — edited Jan 21 2008
i get this error when i try to bubble sort my array list of objects by a string variable in the object:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at iContainer.sort(iContainer.java:90)
at main.main(main.java:9)

This is my sort:
public void sort()
	{
		for (int i=0; i<a.size(); i++) 
		{
  			for (int j=0; j<a.size(); j++)
			{
    				if ((a.get(j).type).compareTo(a.get(j+1).type) > 0)
				{
					instrument tmp = a.get(j);
					a.remove(j);
					a.add(j,a.get(j+1));
					a.add(j+1,tmp);
				}
      					
			}
  		}
	}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 18 2008
Added on Jan 21 2008
5 comments
1,409 views