• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

BinarySearch()

 
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question no. 9 chapter 7 k&B

Given a properly prepared String array containing five elements, which range of results could a proper invocation of Arrays.binarySearch()produce?
answer: -6 through 4

per my understanding answer should be (-5 through 4) since inserion points evaluates as (-(insertion point) -1), so for eg last insertion point of index 4 should be (-4 -1) which is -5 Not -6
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not all person may have the book with them while reading posts.
Please make a practice to post the questions also.
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sanjeev Singh:
Not all person may have the book with them while reading posts.
Please make a practice to post the questions also.



There is no actual question, only the statement that I mentioned.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers,

Ahmed wrote

per my understanding answer should be (-5 through 4) since inserion points evaluates as (-(insertion point) -1), so for eg last insertion point of index 4 should be (-4 -1) which is -5


No, the last insertion point is behind the last entry, so if you have a sorted collection say:
(0, 1, 2, 3, 4)

and you do a binary search for number 5, which isn't in. Then the insertion point would be index 5 (after the four)
And as the returned value of a not-find is always
-(insertion point - 1)
the binary search should return -6.



Yours,
Bu.
[ July 19, 2007: Message edited by: Burkhard Hassel ]
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

binarySearch(List<? extends Comparable<? super T>>

checkedMap(Map<K,V> m, Class<K> keyType,)

checkedSortedSet(SortedSet<E> s, Class<E> type)

these methods were taken from the collections class

MY doubt is the arguments
1.List<? extends Comparable<? super T>
2.checkedMap(Map<K,V> m, Class<K> keyType,)
3.(SortedSet<E> s, Class<E> type)

In this forum i read that <E> is for numeric types is it true?


and please explain what these arugments types expect.
 
Burkhard Hassel
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy ranchers!

Saravana asked:

In this forum i read that <E> is for numeric types is it true?


No. You must have misunderstood something. It is a generic type. On the Sun site is a tutorial part also about Generics. You'll need to spend some time with this for the exam.

Yours,
Bu.
 
Opportunity is missed by most people because it is dressed in overalls and looks like work - Edison. Tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic