Results 1 to 9 of 9
  1. #1
    jetnor is offline Member
    Join Date
    Jan 2011
    Location
    London
    Posts
    17
    Rep Power
    0

    Default 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 follow
    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));
    	   }
    	  }
    By reading about Lists on the net I am in a bit of a dilema whether it is possible at all.

    Thanks in advance for any help.
    Jetnor.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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[]?

  3. #3
    jetnor is offline Member
    Join Date
    Jan 2011
    Location
    London
    Posts
    17
    Rep Power
    0

    Default

    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.
    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));
    	   }
    	  }
    	
    
    	
    }
    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.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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.

  5. #5
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    ok, the method convertToDouble could look like that:

    Java 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;
    		  }
    and for example you can call it by

    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.

  6. #6
    jetnor is offline Member
    Join Date
    Jan 2011
    Location
    London
    Posts
    17
    Rep Power
    0

    Default

    I understand what you are saying but I don't know how can you iterate through a List to parse to double.
    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;
    	  }
    Thanks for your time.

  7. #7
    jetnor is offline Member
    Join Date
    Jan 2011
    Location
    London
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by j2me64 View Post
    ok, the method convertToDouble could look like that:

    Java 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;
    		  }
    and for example you can call it by

    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.
    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:
    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]);
    		}
    Thanks very much, Jetnor.

  8. #8
    jetnor is offline Member
    Join Date
    Jan 2011
    Location
    London
    Posts
    17
    Rep Power
    0

    Default

    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:
    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
    Happy Coding.
    Jetnori.

  9. #9
    opy01 is offline Member
    Join Date
    Nov 2011
    Posts
    1
    Rep Power
    0

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

    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
    Then I populate my List<Double[]> with this:

    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();
    		}
    Eclipse added the try/catch for me.

    Thanks again for the great code. I would not have gotten my app working without it.

Similar Threads

  1. convert string to list
    By isme in forum New To Java
    Replies: 19
    Last Post: 06-16-2010, 09:01 AM
  2. Java double linked list
    By Clown in forum New To Java
    Replies: 1
    Last Post: 05-07-2010, 04:04 PM
  3. Circular Double Linked List
    By theonly in forum Advanced Java
    Replies: 3
    Last Post: 12-06-2009, 05:10 PM
  4. Convert Linked List Object element to String
    By CirKuT in forum New To Java
    Replies: 2
    Last Post: 12-13-2008, 05:22 AM
  5. convert String to Double
    By azurovyhrosik in forum CLDC and MIDP
    Replies: 5
    Last Post: 10-22-2008, 02:46 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •