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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:40:13+00:00 2026-05-28T06:40:13+00:00

Why does it always return 49999 no matter what strToSearch variable holds? Even with

  • 0

Why does it always return 49999 no matter what strToSearch variable holds? Even with the clank search variable it returns the same. Have I missed something:

    String[] arr = new String[100000];
    String strToSearch = "12";
    for (int i = 0; i < arr.length; i++) {
        arr[i] = i+","+i;
    }
    Arrays.sort(arr, new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            if(o1 != null && o2 != null && !o1.isEmpty() && !o2.isEmpty() && o1.indexOf(",") != -1 && o2.indexOf(",") != -1) {
                String[] o1Arr = o1.split(",");
                String[] o2Arr = o2.split(",");
                Integer one = Integer.parseInt(o1Arr[0]);
                Integer two = Integer.parseInt(o2Arr[0]);
                return one.compareTo(two);
            }
            return 0;
        }
    });
    System.out.println(Arrays.binarySearch(arr, strToSearch, new Comparator<String>() {
        @Override
        public int compare(String o1, String o2) {
            if(o1 != null && o2 != null && !o1.isEmpty() && !o2.isEmpty() && o1.indexOf(",") != -1 && o2.indexOf(",") != -1) {
                String[] o1Arr = o1.split(",");
                String[] o2Arr = o2.split(",");
                return o1Arr[0].compareTo(o2Arr[0]);
            }
            return 0;
        }
    }));

My array contains comma separated values and I want to search based on string before comma in array elements. Is there any other simple solution available? I have also made a custom method which loops over array and finds the string but I’m looking for some alternatives.

  • 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-28T06:40:14+00:00Added an answer on May 28, 2026 at 6:40 am

    The JavaDoc on Arrays.binarySearch(...) states that the array must already be sorted and thus the comparator actually compares the array value and the search string and not is not used for sorting the array again.

    This means you get something like compare(arr[x], "12") and your if condition states that both strings must contain a comma, otherwise they are equal. And "12" doesn’t contain any commas, thus making "12" equal to every element in the array (always returns 0).

    Edit:

    Looking at the source code seems to support my assumption.
    There’s this line, for example:

     //c is the Comparator
     //midVal is an element of your array 
     //key is the key parameter you passed to binarySearch(...)
     c.compare(midVal, key); 
    

    The reason for 49999 being returned is that the first mid value is at index mid = (low + high) >> 1 which is (0 + 99999) >> 1 == 49999 and since the comparator returns 0 the values are considered equal. And boom, the value is “found”.

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

Sidebar

Related Questions

For the same input structure, will fieldnames always return the same cell array, even
Why does Windows SendMessage() always return ZERO, even the message delivery is success? Is
Why does the following code always return true even when an exception is thrown?
Why does Thunderbird always return immediately after sending mail through MAPI? Other clients, like
Why does this code always return 0? var possibleMatches = new Array(); $.getJSON('getInformation.php', function(data)
Does fgets() always terminate the char buffer with \0 even if EOF is already
The documentation of CharUnicodeInfo.GetUnicodeCategory says: Note that CharUnicodeInfo .GetUnicodeCategory does not always return the
inside of initWithConder self.superview will always return (null) for me. How does one get
Why does left(FIELD, replace(nullif(charindex('-', FIELD), 0), null, len(FIELD))) always return null? The idea behind
What do you think , does the Stored Procedure always return 1 ? I

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.