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!!
> 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.
>
> 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...
> .....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 >

> 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--