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 6679751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:23:42+00:00 2026-05-26T04:23:42+00:00

Hello I am writing this program, and I am trying to figure out how

  • 0

Hello I am writing this program, and I am trying to figure out how to compare two items in an array to find the largest item.

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]>a[i+1])
            arrCopy[i]=a[i];
        else
            arrCopy[i]=a[i+1];
    }
    return arrCopy[0];
}

But then I don’t know how to test for it, I tried :

T[] a = {1,12,7,45,22,23,5};
    System.out.println("Array: [1,12,7,45,22,23,5] low=0 high, Largest?: " + rec.getLargest(a, 0, 6));

but I get an error message

The method getLargest(T[], int, int) in the type Rec is not applicable for the arguments (int[], int, int)

How do I call it to make it an array of numbers? Would an array of strings work with the code I have for getting the largest?

Maybe is just simple answers but I’ve been working on the whole program for a while and things don’t look so clear now.

EDIT

After changing the array from int[] to Integer[]. I am getting an error on this line if(a[i]>a[i+1])
saying

The operator > is undefined for the argument type(s) T, T

I’m assuming I would have to change the > sign to compare elements in the array, but how do I do this? Use compareTo() ?

  • 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-26T04:23:42+00:00Added an answer on May 26, 2026 at 4:23 am

    It’s not working because int isn’t a subclass of Comparable because it’s not even an Object. Basically, you can’t use primitives (int, short, long, char, boolean, byte) with generics.

    Try calling it with an array of Integer.

    Integer[] a = {1,12,7,45,22,23,5};
    System.out.println("Array: [1,12,7,45,22,23,5] low=0 high, Largest?: " + rec.getLargest(a, 0, 6))
    

    If you want your method to be able to accept int[], you’ll need to have a separate function for each one you want to accept (int getLargest(int[], int, int), char getLargest(char[], int, int), etc.).

    EDIT:

    In response to your edit, you need to use compareTo(). < is only defined for primitives.

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

Sidebar

Related Questions

This is a program I'm writing that's supposed to display some text in a
SOLVED see Edit 2 Hello, I've been writing a Perl program to handle automatic
Hello I'm an amateur trying to learn/improve my understanding of Java by writing a
Hello I wish not to disturb but I'm newer to developing program. I'm trying
Hello guys I'm writing a test file for a program. where all the possible
I'm in the process of writing an assembly program that takes two strings as
Hello I'm writting junit test how can I test this method .. this is
Hello I was writing a Regular Expression (first time in my life I might
Hello Im currently writing a product syncronisation script for magento. I know how to
Hello I am writing some data structures in C, and I've realized that their

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.