ArrayList, casting to Double[]

Hello:

I'm trying to convert an ArrayList to a Double[] . I'm still stuck in this simple thing and I need to keep going, so if there is someone out there that can help me I will appreciate it. I would prefer if it is in a single line of code, somethingelegant: that I may not need to "travel" through all the items of the arrayList...

This is the last thing I tried:

Double[] dcosxArray = (Double[])dcosx.toArray(new Double[dcosx.size()]);

.....wheredcosx is the ArrayList andDouble[] dcosxArray is the array ..

The exception is giving me is:

java.lang.ArrayStoreException

java.lang.System.arraycopy(Native Method)

java.util.ArrayList.toArray(ArrayList.java:306)

Thanks!!

[799 byte] By [tofuwithcoffeea] at [2007-11-27 9:28:05]
# 1
Double[] dcosxArray = (Double[])dcosx.toArray(new Double[dcosx.size()]);This will work if dcosx holds only Doubles.
jverda at 2007-7-12 22:31:47 > top of Java-index,Java Essentials,Java Programming...
# 2

> This is the last thing I tried:

>

> Double[] dcosxArray =

> (Double[])dcosx.toArray(new

> Double[dcosx.size()]);

>

> .....where dcosx is the ArrayList and

> Double[] dcosxArray is the array ..

What was wrong with it? That should work.

hunter9000a at 2007-7-12 22:31:47 > top of Java-index,Java Essentials,Java Programming...
# 3

>

> The exception is giving me is:

> java.lang.ArrayStoreException

> java.lang.System.arraycopy(Native Method)

> java.util.ArrayList.toArray(ArrayList.java:306)

Ok, that ArrayList if being "filled" this way:

for(i = 0; i < nangle; i++)

{

dcosx.add(allTokens.get(i));

}

.....where allTokens is another ArrayList that is being "filled" this way:

while ( ( l = bReader.readLine() ) != null)

{

StringTokenizer lineTokens = new StringTokenizer(l);

while (lineTokens.hasMoreTokens())

{

allTokens.add(lineTokens.nextElement());

}

}

...I am reading a file which each line has several decimals separated by spaces, that is why I chose to use that StringTokenizer up there...

...I need to convert that ArrayList, specifically, to an array because I will use it later with a package named JAMA that works with matrix multiplication, linear algebra...

tofuwithcoffeea at 2007-7-12 22:31:47 > top of Java-index,Java Essentials,Java Programming...
# 4
.....Perhaps in this code line...dcosx.add(allTokens.get(i));...where I add the items to the dcosx ArrayList... I need to convert each item I get() to Double ?.... Let me see.. I'll reply in a nano-second..
tofuwithcoffeea at 2007-7-12 22:31:47 > top of Java-index,Java Essentials,Java Programming...
# 5

> .....Perhaps in this code line...

> dcosx.add(allTokens.get(i));

>

> ...where I add the items to the dcosx ArrayList... I

> need to convert each item I get() to Double ?....

Yes. If you want to get Doubles out of the list, you have to put Doubles in.

jverda at 2007-7-12 22:31:47 > top of Java-index,Java Essentials,Java Programming...
# 6

> Yes. If you want to get Doubles out of the list, you

> have to put Doubles in.

Ok I ended this fight with this:

for(i = 0; i < nangle; i++)

{

dcosx.add(Double.parseDouble(allTokens.get(i).toString()));

}

--Thanks--

tofuwithcoffeea at 2007-7-12 22:31:47 > top of Java-index,Java Essentials,Java Programming...