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

  • SEARCH
  • Home
  • 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 6685871
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:06:26+00:00 2026-05-26T05:06:26+00:00

I am trying to figure out how to compare two items within a T[]

  • 0

I am trying to figure out how to compare two items within a T[] array, here is what I have:

public static <T extends Comparable< ? super T>> T getLargest(T [] a, int low, 
               int high){
    if(low>high)
            throw new IllegalArgumentException();
    T[] arrCopy = (T[]) new Object[high-low];
    for(int i=low;i<high;i++){
        if(a[i].compareTo(a[i-1])>0)
            arrCopy[i]=a[i];
        else
            arrCopy[i]=a[i+1];
    }
    return arrCopy[0];
}

and then I get the error: Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;

Any ideas on how I can resolve this?

  • 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-05-26T05:06:27+00:00Added an answer on May 26, 2026 at 5:06 am

    You can assign the array like this:

    @SuppressWarnings("unchecked")
    T[] arrCopy = (T[]) Array.newInstance(a.getClass().getComponentType(), high-low);
    

    Although the unchecked warning is necessary, this should actually be safe.

    Btw, if you want to find the largest element in an array, here’s a oneliner:

    public static <T extends Comparable<T>> T max(final T[] data) {
        return Collections.max(Arrays.asList(data));
    }
    

    For the complete problem you can use one of these two (they are equivalent):

    public static <T extends Comparable<T>> T maxA(final T[] data,int from, int to) {
        return Collections.max(Arrays.asList(Arrays.copyOfRange(data, from, to)));
    }
    public static <T extends Comparable<T>> T maxB(final T[] data,int from, int to) {
        return Collections.max(Arrays.asList(data).subList(from, to));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm trying to figure out how to compare two jQuery objects, to see
I'm trying to figure out how to compare two address fields I am pulling
I'm trying to compare two dates in PHP and figure out how many days
I was recently trying to figure out the fastest way to compare two large
trying to figure out why this is happening - I have an input text
I have have two list and and I am trying to compare differences in
I have the following query, which is trying to figure out the percentage of
I'm using PowerShell and I'm trying to figure out how to compare the hashes
so I'm trying to figure out a way to compare each and every row
I'm trying to figure out how to compare an n number of lists to

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.