permaculture playing cards
The moose likes Beginning Java and the fly likes ArrayList question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "ArrayList question" Watch "ArrayList question" New topic

ArrayList question

V Bose
Ranch Hand

Joined: Jul 10, 2003
Posts: 113
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));
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Indeed. Why not use the static sort() methods in the java.util.Arrays class instead?

[Jess in Action][AskingGoodQuestions]
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
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




Java API Documentation
The Java Tutorial
Mark Spritzler

Joined: Feb 05, 2001
Posts: 17276

"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


Thanks! and welcome to the JavaRanch!


Perfect World Programming, LLC - iOS Apps
How to Ask Questions the Smart Way FAQ
I agree. Here's the link:
subject: ArrayList question
It's not a secret anymore!