• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ArrayList question

 
V Bose
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do I stuff all the elements of a double array into a Collection and then sort them, in Java 1.5

The following throws a runtime class cast exception...

.
.
.
double[] mArray;
.
.
.
List mList = new ArrayList(Arrays.asList(mArray));
Collections.sort(mArray)
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed. Why not use the static sort() methods in the java.util.Arrays class instead?
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, I'd like to say that Ernest's suggestion is the best since you don't have to create any extra objects. With that said, I'm surprised that your code compiles! Collections.sort() takes a List argument not an array. You can fix this by doing

instead.

HTH

Layne
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"V Bose"-
Welcome to the JavaRanch! Please adjust your displayed name to meet the

JavaRanch Naming Policy.

I am assuming here, but V Bose is very close to Verbose, that is why I believe that this might not be your real name.

You can change it

here.

Thanks! and welcome to the JavaRanch!

Mark
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic