• Post Reply Bookmark Topic Watch Topic
  • New Topic

doubts regarding java.util.Arrays  RSS feed

 
asha ganapathy
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have few doubts regarding the above code:
1) Why does the search return -1 when "one" is searched in an array of
reverse ordered Strings ?
2) I understand the concept of static modifier that its owned by a class
and not an object.To use a static method we use the Classname instead
of instantiating an object.
What i have not understood is:
-> what is the use of a static method or variable or a static class?
Cant we perform the same operations using objects?

Thanks in advance
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) Because binarySearch() only works on an array that is sorted according to the natural ordering of the objects in the array. The API documentation for the method Arrays.binarySearch() explains in detail what that means - look it up. It will not work correctly if the array is not sorted according to the natural ordering, as in the case in your code above.

2) See Understanding Instance and Class Members in The Java Tutorial.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!