This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

confused about collections

 
thea melianta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
import java.util.*;
class SearchObjArray {
public static void main(String [] args) {
String [] sa = {"one", "two", "three", "four"};
Arrays.sort(sa); // #1
for(String s : sa)
System.out.print(s + " ");
System.out.println("\none = "+ Arrays.binarySearch(sa,"one")); // #2

System.out.println("now reverse sort");
ReSortComparator rs = new ReSortComparator(); // #3
Arrays.sort(sa,rs);
for(String s : sa)
System.out.print(s + " ");
System.out.println("\none = "+ Arrays.binarySearch(sa,"one")); // #4

System.out.println("one = "+ Arrays.binarySearch(sa,"one",rs)); // #5
}

static class ReSortComparator
implements Comparator<String> { // #6
public int compare(String a, String b) {
return b.compareTo(a); // #7
}
}
}

which produces something like this:
four one three two
one = 1
now reverse sort
two three one four
one = -1 --> ???
one = 2

can somebody explain how that code produce "one = -1"?
and what are the differences between Arrays.binarySearch(sa,"one") which produce -1 and Arrays.binarySearch(sa,"one",rs) which produce 2, according that codes?

from Page 557-558(Ch VII) of the book titled "SCJP Sun Certified Programmer for Java 5 Study Guide (Exam 310-055)"
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
when you are using binarySearch method prior to call of this method array must be sorted.

in line 4 you are not getting proper value because before reaching this point you sorted array using comparator,and you are not providing same comparator when you are searching.
If you provide same comparator it will result in correct value,that is what happening in line 5
 
thea melianta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i'm sori but i'm still confused..

how about in line 2??
it uses same syntac "Arrays.binarySearch(sa,"one")" but produces 1?
is it because of this syntacs -->
ReSortComparator rs = new ReSortComparator(); // #3
Arrays.sort(sa,rs); ??

thanks
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
line 2 produces proper result because in line 1, you are using Arrays.sort() method to sort array, which will sort array in natural order.

If you are using any comparator then you need to provide the same in Arrays.binarySearch().
[ August 31, 2008: Message edited by: ramesh maredu ]
 
thea melianta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
owww..

so if i use Arrays.sort("array","comparator"), i have to use Arrays.binarySearch("array","key","comparator")? if i dont use it, i will get -1??
am i right??
 
ramesh maredu
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes you need to use comparator,
I think the result will be uncertain if we don't use Comparator,I am not sure about this.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kenshin, please check your private messages. You can see them by clicking My Private Messages.
 
thea melianta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi it's me again with the same topic..
i do a little some changes in the code line 4

before
System.out.println("\none = "+ Arrays.binarySearch(sa,"one")); // #4

after
System.out.println("\none = "+ Arrays.binarySearch(sa,"one"));
System.out.println("\ntwo = "+ Arrays.binarySearch(sa,"two"));
System.out.println("\nthree = "+ Arrays.binarySearch(sa,"three"));
System.out.println("\nfour = "+ Arrays.binarySearch(sa,"four"));

and the results are
one = -1

two = -5

three = 1-->strange or truly right?

four = -1

why three can produces 1, which means the element successful found in the list?

can anyone explain it?
thanks before..





 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can anyone explain it?
thanks before..


There is nothing to explain. The JavaDoc clearly states that the results are undefined if the array is not sorted. It may not find it, even if the item is in the array. Or it may find it, just by dumb luck.

Henry
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes! If you search in an unsorted array or search with a different comparator then what you used for sorting, then you get uncertain result.

There is a similar question in the practice question of the K&B book. It's explanation states that you don't need to know what the exact output will be in case of an uncertain result for SCJP...

So the bottom line is, if you search in an unsorted array or if you search on an array which was sorted using a comparator and you don't use that comparator for searching, then for that you will choose output uncertain as the correct option in the exam...
 
thea melianta
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok..
thank you..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic