Results 1 to 9 of 9
- 02-06-2011, 09:49 PM #1
Member
- Join Date
- Jan 2011
- Location
- London
- Posts
- 17
- Rep Power
- 0
How can you convert a List<String[]> to a List<Double[]>?
Hi Guys,
I am trying to convert a List of String arrays to a list of double arrays. What I am doing is reading a coma seperated value file and the output from my ReadFile method is a List<String[]>. The file contains numbers of type double but when I do the reading type String is returned. Now I want to do data analysis to these numbers and hence need to convert them to double. My attempt which doesn't work is as followBy reading about Lists on the net I am in a bit of a dilema whether it is possible at all.Java Code:List<String[] allElements; /** * Convert the string arrays to double arrays */ public List <double[]> ConvertToDouble() { double[] arrDouble = new double[allElements.size()]; for(int i=0; i<allElements.size(); i++) { arrDouble[i] = Double.Parse(allElements.get(i)); } }
Thanks in advance for any help.
Jetnor.
-
Your method states that it will return a List of double arrays, and it doesn't do that -- in fact it returns nothing. Why do you want it to return a list of double arrays? Don't you want it to return either a List<Double> or a double[]?
- 02-06-2011, 10:05 PM #3
Member
- Join Date
- Jan 2011
- Location
- London
- Posts
- 17
- Rep Power
- 0
Hi Fubarable,
Thanks for your reply. If you could have a look at my class and maybe you will be able to help by seeing the whole thing.So basically my first method ReadFile returns a List<String[]> which on my understanding means a list collection of String arrays. These string arrays are in fact numbers so I want to convert them to double arrays so I can do maths on them. More particularly each String[] element of the list is a variable in a dataset and I want to calculate Pearson's correlation between each of these variables. The problem is I don't know how to do the converting. Thanks, Jetnor.Java Code:import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Arrays; import java.util.List; import au.com.bytecode.opencsv.CSVReader; public class ReadFile { List<String[]> allElements; private static final String ADDRESS_FILE="D:\\faith.csv"; /** * @param args * @throws FileNotFoundException */ public List<String[]> readFile() throws IOException { CSVReader reader = new CSVReader(new FileReader(ADDRESS_FILE)); reader.readAll(); return allElements; } /** * Convert the string arrays to double arrays */ public List <double[]> ConvertToDouble() { double[] arrDouble = new double[allElements.size()]; for(int i=0; i<allElements.size(); i++) { arrDouble[i] = Double.Parse(allElements.get(i)); } } }
-
Again your ConvertToDouble method doesn't live up to its signature, again it states that it is to return a List of double arrays, and it doesn't do this, but more importantly, you don't want it to do this. All you need is the double array, so why not declare it to return a double array (no need for a List here), and then do this at the end of the method with a return statement? All you have to do is change the method signature so that the return type is double[] and then add a line at the end to return the array of double that you are creating in the method.
- 02-06-2011, 10:15 PM #5
ok, the method convertToDouble could look like that:
and for example you can call it byJava Code:public static double[] convertToDouble(String[] elems) { double[] arrDouble = new double[elems.length]; String[] string = allElements.get(0); for(int i=0; i<elems.length; i++) { arrDouble[i] = Double.valueOf(elems[i]); } return arrDouble; }
Java Code:allElements = new ArrayList<String[]>(); String[] s1 = new String("1.5, 1.9, 2.0, 3.0").split(","); allElements.add(s1); double[] converted = convertToDouble(allElements.get(0));
and the variable converted will now contain the doubles passed as string. the code above works only for one double array, but you can change it easily for passing and returning a list of double arrays.Last edited by j2me64; 02-06-2011 at 10:17 PM.
- 02-06-2011, 10:21 PM #6
Member
- Join Date
- Jan 2011
- Location
- London
- Posts
- 17
- Rep Power
- 0
I understand what you are saying but I don't know how can you iterate through a List to parse to double.
Thanks for your time.Java Code:/** * Convert the string arrays to double arrays */ public double[] ConvertToDouble() { double[] arrDouble = new double[allElements.size()]; for(int i=0; i<allElements.size(); i++) { arrDouble[i] = Double.parseDouble(/**what do I put here*/); } return arrDouble; }
- 02-06-2011, 11:27 PM #7
Member
- Join Date
- Jan 2011
- Location
- London
- Posts
- 17
- Rep Power
- 0
Hello mate,
Thanks for reply. I am getting closer to understanding the problem but unfortunately still haven't solved it. I have updated the convertToDouble method so it iterates through each element in my original list of arrays and another for loop to iterate through each element of the arrays itself.
To know it is working I am trying to print the first array of doubles but just can't seem to do it because I get a null pointer exception on my main method.
Any ideas why this might be happening.
My updated method method for convertToDouble now is:Thanks very much, Jetnor.Java Code:public static double[] convertToDouble(String[]elems) { double[] arrDouble = new double[elems.length]; //iterate through each element in the List allElements for(int i =0; i<allElements.size();i++){ String[] string = allElements.get(i); //iterate through the individual string arrays and put them into a double array for(int y=0; y<string.length; y++) { arrDouble[y] = Double.valueOf(elems[y]); } } //return array of doubles return arrDouble; } public static void main(String[]args) throws IOException{ readFile(); double[] converted = convertToDouble(allElements.get(0)); for(int i = 0; i<converted.length; i++) System.out.println(converted[i]); }
- 02-12-2011, 10:02 PM #8
Member
- Join Date
- Jan 2011
- Location
- London
- Posts
- 17
- Rep Power
- 0
Hi Guys,
I have finally figured it out. I thought I should post a working solution for anybody who is trying to convert a List of String[] to a List of double[]. The code for my method is:
Happy Coding.Java Code:public static List<double[]> ConvertToDouble() throws IOException, InterruptedException, InvocationTargetException { List<String[]> allElements = readFile(); List<double[]> ListOfDoubleArr = new ArrayList<double[]>(); for (Iterator<String[]> it = allElements.iterator(); it.hasNext();) { String[] stringArray = it.next(); double[] doubleArray = new double[stringArray.length]; for (int y = 0; y < stringArray.length; y++) { doubleArray[y] = Double.parseDouble(stringArray[y]); }//end for loop which iterates over each String array to convert them to double arrays ListOfDoubleArr.add(doubleArray); }//end for loop which iterates over List<String[]> return ListOfDoubleArr; }//end method
Jetnori.
- 11-04-2011, 08:30 PM #9
Member
- Join Date
- Nov 2011
- Posts
- 1
- Rep Power
- 0
Re: How can you convert a List<String[]> to a List<Double[]>?
Hey jetnor, thanks for the info. I needed this exact thing and it worked perfect with one small edit.
Then I populate my List<Double[]> with this:Java Code:public static List<Double[]> ConvertToDouble() throws IOException, InterruptedException, InvocationTargetException { List<Double[]> ListOfDoubleArr = new ArrayList<Double[]>(); //my_strings is my List<String[]> populated before this code for (Iterator<String[]> it = my_strings.iterator(); it.hasNext();) { String[] stringArray = it.next(); Double[] doubleArray = new Double[stringArray.length]; for (int y = 0; y < stringArray.length; y++) { doubleArray[y] = Double.parseDouble(stringArray[y]); }//end for loop which iterates over each String array to convert them to double arrays ListOfDoubleArr.add(doubleArray); }//end for loop which iterates over List<String[]> return ListOfDoubleArr; }//end method
Eclipse added the try/catch for me.Java Code:try { values = ConvertToDouble(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (InvocationTargetException e1) { // TODO Auto-generated catch block e1.printStackTrace(); }
Thanks again for the great code. I would not have gotten my app working without it.
Similar Threads
-
convert string to list
By isme in forum New To JavaReplies: 19Last Post: 06-16-2010, 09:01 AM -
Java double linked list
By Clown in forum New To JavaReplies: 1Last Post: 05-07-2010, 04:04 PM -
Circular Double Linked List
By theonly in forum Advanced JavaReplies: 3Last Post: 12-06-2009, 05:10 PM -
Convert Linked List Object element to String
By CirKuT in forum New To JavaReplies: 2Last Post: 12-13-2008, 05:22 AM -
convert String to Double
By azurovyhrosik in forum CLDC and MIDPReplies: 5Last Post: 10-22-2008, 02:46 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks