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!

Generic Array ClassCastException

807589Jun 1 2008 — edited Oct 3 2008
Hi - I am trying to create a MergeSort class. In order to do so, I have to create a temporary generic array in MergeSortedHalves in order to copy the elements in to. I know in order to create a generic array one must create an object array and cast it to a generic array. However, I cant find what I am doing wrong. I have created generic arrays before fine, but im having trouble with this one.
	private static <Type extends Comparable<? super Type>> void mergeSortedHalves(Type[] arr, int left, int middle, int right) {
		Type [] temp = (Type []) new Object[right - left + 1];
                 ...
        }
I get the following exception:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object;\

Any hints on what I am doing wrong? Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 31 2008
Added on Jun 1 2008
3 comments
487 views