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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:28:12+00:00 2026-06-03T08:28:12+00:00

Here is my simple piece of code, where I build a String array and

  • 0

Here is my simple piece of code, where I build a String array and try to search a string within this array:

String[] arr = new String[5];
arr[0] = "ccc";
arr[1] = "aaa";
arr[2] = "bbb";
arr[3] = "eee";
arr[4] = "ddd";

System.out.println(Arrays.binarySearch(arr,"eee"));

Taken directly from Java 6 binarySearch documentation:

The array must be sorted prior to making this call. If it is not
sorted, the results are undefined

Actually, I ran my code several time getting as output always 3 which is the position of eee in my not sorted array, but the result seems not to be "undefined" as the documentation said.

What am I missing?

  • 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-03T08:28:14+00:00Added an answer on June 3, 2026 at 8:28 am

    “Undefined” means that the algorithm will run on your array, but the result is not guaranteed to be correct (binary search strongly needs a sorted array in order to work). Your example works because this is what happens:

    • enter binary search with first = 0, last = 4, middle = 2 compare
    • array[middle] with “eee” (“bbb”<“eee”) => first = 2 + 1; middle = 3;
    • compare array[middle] with “eee” => “found” ; return 3;
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

See this simple piece of code in PHP: //Documentation: //memcache_set ( string $key ,
Here is a simple piece of code: import java.io.*; public class Read { public
Here I provide simple piece of code. function GetStringList:TStringList; var i:integer; begin Result:=TStringList.Create; Result.Add('Adam');
I wrote this simple piece of code(it actually calculates sine, cosine or tangent values
I have this simple html piece of code, whenever I click on any of
I have this simple piece of code that returns what's in the title. Why
Here a simple C# piece of code: Convert.ToInt32(TimeSpan.FromMinutes(5).TotalMilliseconds); //which brings me 300000 (int)TimeSpan.FromMinutes(5).Milliseconds; //which
I cannot get this simple piece of code to compile without including the TestClass.cpp
Here is a very simple piece of jQuery code: $(document).ready(function() { alert('document ready was
Hi this is a very simple piece of code I just write for C++

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.