wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes confused about collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "confused about collections" Watch "confused about collections" New topic
Author

confused about collections

thea melianta
Greenhorn

Joined: Aug 31, 2008
Posts: 9
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

Joined: Mar 15, 2008
Posts: 210

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


SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
thea melianta
Greenhorn

Joined: Aug 31, 2008
Posts: 9
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

Joined: Mar 15, 2008
Posts: 210

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

Joined: Aug 31, 2008
Posts: 9
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

Joined: Mar 15, 2008
Posts: 210

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

Joined: Aug 16, 2005
Posts: 14074
    
  16

kenshin, please check your private messages. You can see them by clicking My Private Messages.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
thea melianta
Greenhorn

Joined: Aug 31, 2008
Posts: 9
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
Sheriff

Joined: Sep 28, 2004
Posts: 18542
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
thea melianta
Greenhorn

Joined: Aug 31, 2008
Posts: 9
ok..
thank you..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: confused about collections
 
Similar Threads
Comparator interface
Searching Arrays and Collections
Comparator interface
sorting array
Comparator's compare() method question