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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:29:07+00:00 2026-06-03T18:29:07+00:00

I have seen examples on the site that deal with generics with multiple parameters

  • 0

I have seen examples on the site that deal with generics with multiple parameters but none that work for my situation.

So here is the deal: I am trying to learn Java generics and have decided to create a simple binary array search utility function. I am testing it out using custom objects and integers. To get feedback on errors and warning I am using Eclipse. Here is what I have:

public static int binarySearch(Comparable[] array, Comparable item, int start, int end) {
    if(end < start) {
        return -1;
    }
    int mid = (start + end) / 2;
    if(item.compareTo(array[mid]) > 0) {
        return binarySearch(array, item, mid + 1, end);
    } else if(item.compareTo(array[mid]) < 0) {
        return binarySearch(array, item, start, mid - 1);
    } else {
        return mid;
    }
}

So obviously I get the warnings for Raw types saying the generics should be parameterized. How can I do this correctly given that I have multiple parameters which both need to be the same type?

SOLUTION

Here is the working solution using generics with the correct parameter checks:

public static <T extends Comparable<? super T>> int binarySearch(T[] array, T item, int start, int end) {
    if(array.length == 0) {
        return -1;
    }
    if(item == null) {
        return -1;
    }
    if(start < 0) {
        return -1;
    }
    if(end < start) {
        return -1;
    }
    int mid = (start + end) / 2;
    if(item.compareTo(array[mid]) > 0) {
        return binarySearch(array, item, mid + 1, end);
    } else if(item.compareTo(array[mid]) < 0) {
        return binarySearch(array, item, start, mid - 1);
    } else {
        return mid;
    }
}
  • 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-03T18:29:08+00:00Added an answer on June 3, 2026 at 6:29 pm

    you can specify function-specific generic parameter like so

    public static <T extends Comparable<? super T>> int binarySearch(T[] arr,T elem,int start,int end){
        //...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen oodles of examples to fix IE8's z-index insanity but none of
I've seen a few similar posts to my problem, but none that have helped
I have seen examples of ListView where each row has multiple view objects, like
I have seen examples of printing from a windows application but I have not
Greetings everybody. I have seen examples of such operations for so many times that
I have seen lots of chat examples in Erlang but what about lists, like
I've seen this format function referenced on several sites, but none of them have
I have seen Ext.net's examples site in which each new page is added as
I've seen a lot of examples that site how to use jQuery and .each()
I have seen numerous examples of the Command pattern But they all tend 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.