Collections.shuffle error

440 views
Skip to first unread message

stymie

unread,
Jul 23, 2008, 2:36:59 AM7/23/08
to Google Web Toolkit
Hi,

I do not understand why this code does not work correctly.Failry new
to Java. I've googled and googled some more.


public class Cars implements EntryPoint {
private String[] cars={"ford mustang","chevy cavalier"," buick
century"};
private List<String> theCars= new ArrayList<String>();

public void onModuleLoad() {

theCars=Arrays.asList(cars);
Collections.shuffle(theCars);//Here is where the error is

Line 29: The method shuffle(List<String>) is undefined
for the type Collections

I have imported this import java.util.Collections;

Window.alert(theCars.toString());


}


}

Thanks

Jason Morris

unread,
Jul 23, 2008, 3:46:58 AM7/23/08
to Google-We...@googlegroups.com

Hi there,

GWT doesn't emulate the Collections.shuffle method (I think mainly because it
doesn't emulate Java.util.Random). You could write it fairly easily like this:

import com.google.gwt.user.client.Random;

// ... class declaration etc.

private void swap(List<?> list, int idx1, int idx2) {
Object o1 = list.get(idx1);
list.set(idx1, list.get(idx2));
list.set(idx, o1);
}

public void shuffle(List<?> objects) {
for(int i = objects.size; i > 1; i--) {
swap(list, i - 1, Random.nextInt(i));
}
}


For more info on what standard JRE methods /are/ implemented in GWT, take a look
at the docs here:
http://code.google.com/docreader/#p(google-web-toolkit-doc-1-5)s(google-web-toolkit-doc-1-5)t(RefJreEmulation)

Hope that helps

stymie

unread,
Jul 23, 2008, 4:29:24 PM7/23/08
to Google Web Toolkit
Hi,

I am getting some errors on this code:
> private void swap(List<?> list, int idx1, int idx2) {
> Object o1 = list.get(idx1);
> list.set(idx1, list.get(idx2));
> list.set(idx, o1); // Gives an error about idx not being resloved changed below
>
> }
>
> public void shuffle(List<?> objects) {
> for(int i = objects.size; i > 1; i--) {
> swap(list, i - 1, Random.nextInt(i)); //Gives an error about list not being resolved changed below
> }
>
> }
I made the following changes not sure if its right though
private void swap(List<?> list, int idx1, int idx2) {
Object o1 = list.get(idx1);
list.set(idx1, list.get(idx2));
list.set(idx2, o1); //Changed here to idx2


}

public void shuffle(List<?> objects) {
for(int i = objects.size(); i > 1; i--) {
swap(objects, i - 1, Random.nextInt(i)); //Changed to
objects
}

}

In both of the list.set of the swap method it gives an error along
these lines
The method set(int, capture#4-of ?) in the type List<capture#4-of ?>
is not applicable for the arguments (int, Object)

Thanks
> at the docs here:http://code.google.com/docreader/#p(google-web-toolkit-doc-1-5)s(goog...)
>
> Hope that helps

Jason Morris

unread,
Jul 27, 2008, 8:09:13 AM7/27/08
to Google-We...@googlegroups.com
Sorry about that, wrote the code in my email client. The swap method should look
like this:

private void swap(List<?> list, int idx1, int idx2) {
Object o1 = list.get(idx1);
list.set(idx1, list.get(idx2));
list.set(idx2, o1);
}

While the shuffle method should look like what you had (objects instead of list).

Cheers.

stymie

unread,
Jul 27, 2008, 9:34:41 PM7/27/08
to Google Web Toolkit
Thanks,

I managed to get it working.
Reply all
Reply to author
Forward
0 new messages