Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8654949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T14:54:47+00:00 2026-06-12T14:54:47+00:00

For practice, I’m trying to implement the merge sort algorithm but got stuck pretty

  • 0

For practice, I’m trying to implement the merge sort algorithm but got stuck pretty fast when trying to instantiate a generic type array. I’m not entirely sure if my plan will work out, but right now the interesting part (or frustrating, depending on when you ask me) is the second row in the merge() method. The newInstance() method needs to know what class it should initiate as, but even if arr1.getClass() compiles perfectly fine, it won’t work during runtime.

public void mergeSort(T[] arr) {

    T[] merged = merge(Arrays.copyOfRange(arr, 0, arr.length/2), Arrays.copyOfRange(arr, arr.length/2+1, arr.length-1));

}

@SuppressWarnings({"unchecked"})
public T[] merge(T[] arr1, T[] arr2) {
    // A new array of type T that will contain a merged version of arr1 and arr2
    T[] merged = (T[]) Array.newInstance(arr1.getClass(), arr1.length+arr2.length);

    int i1 = 0, i2 = 0;
    for (int i = 0; i < arr1.length + arr2.length; i++) {
        if (arr1[i1].compareTo(arr2[i2]) < 0) {
            merged[i] = arr1[i1];
            i1++;
        } else {
            merged[i] = arr2[i2];
            i2++;
        }
    }

    return merged;
}

The error message is:

Exception in thread "main" java.lang.ClassCastException: [[Ljava.lang.String; cannot be cast to [Ljava.lang.Comparable;
    at sort.SortingAndSearching.merge(SortingAndSearching.java:94)
    at sort.SortingAndSearching.mergeSort(SortingAndSearching.java:84)
    at sort.SortingAndSearching.main(SortingAndSearching.java:19)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T14:54:48+00:00Added an answer on June 12, 2026 at 2:54 pm

    Think I see the issue… When you do Array.newInstance(...), it takes in the component type (in your case, you want it to be String). However, you’re handing the array class (arr1 is of type String[], and you’re doing arr1.getClass()). Instead, do

    arr1.getClass().getComponentType()
    

    to get the String class out of String[]

    Per javadoc:

     static Object newInstance(Class<?> componentType, int[] dimensions) 
    

    componentType – the Class object representing the component type of the new array

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For practice, I'm trying to define a type corresponding to lambda-calculus expressions composed of
Best practice is to use unique ivs, but what is unique? Is it unique
for practice purposes, I'm trying to find a way to print rows of numbers
For practice, I'm trying to do some stuff in Python. I've decided to make
For practice I am trying to display a number that increments from 0 -
Best practice for REST resource versioning is putting version information into Accept/Content-Type headers of
What's the best practice for leveraging s3 I've got a Rails app that's hosted
As a practice exercise, I am creating my own generic class that is basically
To practice using pointers and arrays i'm trying to do a simple program capable
I am trying to practice making making some xml and also a parser for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.