0

I have list of array of strings, List <String []>, what is the best optimal approach to get contents of this list of array of strings?

public List<String []> readFromString (String data){
  StringReader stringReader = new StringReader(data);
  CVSReader reader = new CVSReader(stringReader);
  return reader.readAll()
}

In above example, I want to see the actual contain of reader.readAll(), any suggestions as to what is the optimal way to get that information out?

Rachel
  • 100,387
  • 116
  • 269
  • 365
  • I don't really understand the question. Do you have a String with embedded linefeeds, each line of which is in CSV format? – Bohemian Jul 14 '11 at 01:18
  • I am passing the csv file to the reader and I want to see if the file is properly read or not and so want to see the contents of `reader.readAll` – Rachel Jul 14 '11 at 01:19
  • Also in general i would like to know as to what would be best way to get content of each array of strings present in the list – Rachel Jul 14 '11 at 01:20
  • Probably you need to investigate StringBuilder. The top answer here has some good thoughts on joining a List: http://stackoverflow.com/questions/1751844/java-convert-liststring-to-a-joind-string - although you would first need to interate over it and join the arrays of strings together first. – Charles Goodwin Jul 14 '11 at 01:21

4 Answers4

1

I don't think there's any avoiding looping through the entire structure. Even if you call .toString(), which may well give what you want, you're still going to incur the cost of looping over the entire data structure:

String results = readFromString(data);

StringBuilder output = new StringBuilder();
for(String[] sArray : results) {
    for(String s : sArray) {
        output.append(s);
    }
}

System.out.println(output);

(Note: insert formatting characters as required - you might want to put a comma after each string, and a \n after each list completes, to make the output more readable.)

Eric
  • 520
  • 1
  • 4
  • 14
1

By wanting to see the content and "get information out", if you mean that you want to send it to standard out, or your log file, to see a full dump of the data, you can use the List toString (i.e. System.out.println(reader.readAll()); ). It prints all values. The following unit test confirms it:

public void testListOfArraysToStringPrintsAllValues(){
        String[] array1 = ["array1.1", "array1.2"];
        String[] array2 = ["array2.1", "array2.2"];

        List<String[]> listOfArrays = new ArrayList<String[]>();
        listOfArrays.add(array1);
        listOfArrays.add(array2);

        assertEquals("[[array1.1, array1.2], [array2.1, array2.2]]", listOfArrays.toString());

}
Moe Matar
  • 2,026
  • 13
  • 7
0

Are you looking for something like this?

for(String[] sArray : new CVSReader(new StringReader(data)).readAll())
{
    for(String s : sArray)
    {
        System.out.prinntln(s);
    }
    System.out.prinntln("*********");
}
Eng.Fouad
  • 115,165
  • 71
  • 313
  • 417
0

I can be wrong but my suggetions are:

  • To separte the lines:

String[] lines = data.split("\n");

  • To get a java.util.List

java.util.Arrays.asList(lines)

  • To separate each line:

String[] fields = line.split(",");

where line will be one of the String[] lines element.

Leonel Martins
  • 2,713
  • 1
  • 21
  • 24
  • CSVReader is a better choice, consider embedded commas, like this data: `"foo", "this, that", "bar"` has **3** columns, but if split on comma would give **4** values – Bohemian Jul 14 '11 at 04:12