Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / January 2005

Tip: Looking for answers? Try searching our database.

shuffle loses/duplicates objects in LinkedList

Thread view: 
Adrian Collister - 19 Jan 2005 19:51 GMT
I'm trying to use the Collections.shuffle() method on a LinkedList. After
the shuffle() the LinkedList is totally mangled, with several of the
original Objects being replaced by duplicates of others. If I use an
ArrayList instead of the LinkedList the behaviour is as expected.

Am I doing something wrong, or is this a bug?

The appended code demonstrates the problem. Example output when I execute
it:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
0 3 7 0 6 13 4 1 12 15 11 6 14 2 7 3 17 14 17 8
0 0 1 2 3 3 4 6 6 7 7 8 11 12 13 14 14 15 17 17

I'm using the GCC java compiler - might be interesting to see what happens
if it's compiled using Sun's compiler.

Adrian

/*---------------- CODE -----------------*/

import java.util.List;
import java.util.LinkedList;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Collections;

public class ShuffleTest
{
 
 public static void main(String[] args)
 {
   // Fill a list with "Int" objects (see below)
   LinkedList x = new LinkedList();
   for (int i = 0; i < 20; ++i) {
     x.add(new Int(i));
   }
   
   // Shuffle then re-sort...
   printList(x);
   Collections.shuffle(x);
   printList(x);
   Collections.sort(x);    
   printList(x);
 }
 
 // Dump list to STDOUT.
 public static void printList(List l)
 {
   Iterator it = l.iterator();
   while (it.hasNext()) {
     System.out.print(((Int)it.next()).value + " ");
   }
   System.out.println();
 }
 
 
 // Simple Class which just contains an int.
 public class Int implements Comparable
 {
     
   int value;
   
   // Constructor
   public Int(int v)
   {
     this.value = v;
   }
   
   public int compareTo(Object o)
   {
     Int i = (Int) o;
     
     if (value < i.value) {
       return -1;
     } else if (value == i.value) {
       return 0;
     } else {
       return 1;
     }
   }
   
 }
}
Stefan Schulz - 19 Jan 2005 20:01 GMT

>   // Simple Class which just contains an int.
>   public class Int
>   implements Comparable {

This should be a static class. javac rightly complains about that.

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
12 1 15 3 10 14 2 5 13 18 6 8 11 17 19 4 0 7 16 9
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Other then what i mentioned above, the program works as expected. As a
wild guess, maybe the gnu compiler provides a class called Int internally,
and this class has a very strange behaviour. Take a look at what
System.out.println(Int.class) prints on your box.

Signature

In pioneer days they used oxen for heavy pulling, and when one ox
couldn't budge a log, they didn't try to grow a larger ox. We shouldn't
be trying for bigger computers, but for more systems of computers.
          --- Rear Admiral Grace Murray Hopper

Adrian Collister - 19 Jan 2005 20:26 GMT
It seems likely that this is a GCC specific bug then.

System.out.println(Int.class) gives me a "Class Int not found" error at
compilation, and I've tried renaming that class with no luck.

Many thanks.

>  
>>   // Simple Class which just contains an int.
[quoted text clipped - 11 lines]
> and this class has a very strange behaviour. Take a look at what
> System.out.println(Int.class) prints on your box.
Stefan Schulz - 19 Jan 2005 21:49 GMT
> It seems likely that this is a GCC specific bug then.
>
> System.out.println(Int.class) gives me a "Class Int not found" error at
> compilation, and I've tried renaming that class with no luck.
>
> Many thanks.

As i said, since you use Int (why not stick with java.lang.Integer by the
way?) in a static context, you need to declare it statically, like
this:

public static class Int // ...

Signature

In pioneer days they used oxen for heavy pulling, and when one ox
couldn't budge a log, they didn't try to grow a larger ox. We shouldn't
be trying for bigger computers, but for more systems of computers.
          --- Rear Admiral Grace Murray Hopper



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.