This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

array . contains () ???  RSS feed

 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,

i have an array of int's. thought its a one-liner to check if a given int is contained in that array. but it isn't.



or am i blind?

any ideas appreceated,

jan
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16007
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the array is sorted, you could use java.util.Arrays.binarySearch(array, key). But if the array isn't sorted, that won't work. Ofcourse you could sort it using java.util.Arrays.sort(array).
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi jesper,

i was already checking the javadoc, but to be honest i did not fully understand the documentation of the return value of binarySearch:


Returns:
index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size(), if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.


are we sure that a returned value >0 implies "found" ? i'm not :-)

cheers,
jan
 
Jan Groth
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Note that this guarantees that the return value will be >= 0 if and only if the key is found.


stupid me.
 
Shaan Shar
Ranch Hand
Posts: 1249
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jan Groth:
hi jesper,
i was already checking the javadoc, but to be honest i did not fully understand the documentation of the return value of binarySearch:
are we sure that a returned value >0 implies "found" ? i'm not :-)


If you have read that then do it again and carefully read last line.

index of the search key, if it is contained in the list; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the list: the index of the first element greater than the key, or list.size(), if all elements in the list are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
.

Hoep your doubts would be clear now.
Please let us know if there are any issues.

[ September 29, 2006: Message edited by: Ankur Sharma ]
[ September 29, 2006: Message edited by: Ankur Sharma ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!