Skip to Main Content

Java Programming

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!

merge sort

807606Apr 22 2007 — edited Apr 23 2007
Im doing the Merge Sort:
class MergeSort extends Sorter_BK {
	public MergeSort(ArrayList<Comparable> list) {
		super(list);
	}
	public void sort() {  
      if (archive.size() <= 1) return;
      ArrayList<Comparable> first = new ArrayList<Comparable>();
      first.add(archive.size() / 2);
      ArrayList<Comparable> second = new ArrayList<Comparable>();
      second.add(archive.size() - first.size());
      System.arraycopy(archive, 0, first, 0, first.size());
      System.arraycopy(archive, first.size(), second, 0, second.size());
      Sorting firstSorter = new Sorting(first);
      Sorting secondSorter = new Sorting(second);
      firstSorter.mergeSort();
      secondSorter.mergeSort();
      merge(first, second);
   }
   private void merge(ArrayList<Comparable> first, ArrayList<Comparable> second) {  
      int iFirst = 0;
      int iSecond = 0;
      int j = 0; 
      while (iFirst < first.size() && iSecond < second.size()) {  
         if ((first.get(iFirst).compareTo(second.get(iSecond))) < 0) {  
            archive.set(j, first.get(iFirst));
            iFirst++;
         }
         else {  
            archive.set(j, second.get(iSecond));
            iSecond++;
         }
         System.out.println(toString());
         j++;
      }
      System.arraycopy(first, iFirst, archive, j, first.size() - iFirst);
      System.arraycopy(second, iSecond, archive, j, second.size() - iSecond);
   }
}
when i call the sort method i get this runtime error using an ArrayList
Exception in thread "main" java.lang.ArrayStoreException
at java.lang.System.arraycopy(Native Method)
at MergeSort.sort(Sorter_BK.java:125)
at Sorter_BK.main(Sorter_BK.java:54)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 21 2007
Added on Apr 22 2007
1 comment
215 views