0

I am using the csvparser library from univocity and am having trouble converting a List object in to an array that would be easily read from.

The method that keeps throwing an ArrayStoreException is:

List<String[]> resolvedData;
String[] array = new String[7000];

public void parseURLs() throws FileNotFoundException {

    CsvParserSettings settings = new CsvParserSettings();
    settings.getFormat().setLineSeparator("\n");
    CsvParser parser = new CsvParser(settings);

    try {
        resolvedData = parser.parseAll(new FileReader("C:\\Users\\kevin.anderson\\Desktop\\book1.csv"));
        resolvedData.toArray(array);
    } catch (Exception ex) {
        System.out.println(ex.toString());

    }
}

Is there a way to get the items out of a List into an array or is there an easy way to iterate thru the items in the List string array?

Thank you

6
  • 2
    How are you going to put String[] values into a String[]? Commented Jan 7, 2016 at 17:14
  • Why do you want the data in an Array? Commented Jan 7, 2016 at 17:17
  • I presume that each line, which is captured by a String[], is captured in the list. The next question would be, what do you intend to do with the result? Commented Jan 7, 2016 at 17:17
  • @TangledUpInBlue: That's how the API mandates it. Commented Jan 7, 2016 at 17:18
  • 2
    Why the downvote? This is a beginner problem, but well stated and backed up by what has been tried. It's also probably an XY Problem but that is typical for beginners and shouldn't merit a downvote. Commented Jan 7, 2016 at 17:47

4 Answers 4

1

I think this is an XY problem. There is no need to convert the data to an array in order to iterate over it, so I'll give the answer I think you're looking for, which is how to iterate easily over a List<String[]> that you're receiving from the CSV parser.

for (String[] currentLine : resolvedData)
{
    // currentLine here is a String[] containing the fields from one CSV record
    // To iterate over the fields
    for (String field : currentLine)
    {
        // do something with each field
    }
 // --- OR ---
    // To access fields by index
    String firstField = currentLine[0];
    // do something with firstField
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this.

    List<String[]> resolvedData;
    String[][] array;

    public void parseURLs() throws FileNotFoundException {

        CsvParserSettings settings = new CsvParserSettings();
        settings.getFormat().setLineSeparator("\n");
        CsvParser parser = new CsvParser(settings);

        try {
            resolvedData = parser.parseAll(new FileReader("C:\\Users\\kevin.anderson\\Desktop\\book1.csv"));
            array = new String[resolvedData.size()][];
            resolvedData.toArray(array);
        } catch (Exception ex) {
            System.out.println(ex.toString());
        }
    }

Basic errors you assumed a fixed size of the incoming list of 7000 and also a single dimension array which is addressed with

String[][] array = new String[resolvedData.size()][];

Comments

0

You get this ArrayStoreException because the list resolvedData contains String[] elements and not String.

See https://docs.oracle.com/javase/7/docs/api/java/lang/ArrayStoreException.html for details.

Comments

0

@Daruisz is right. resolvedData.toArray() will produce an array of arrays:

    String[] array1 = {"1a", "1b"};
    String[] array2 = {"2a", "2b"};
    String[] array3 = {"3a", "3b", "3c"};
    List<String[]> data = Arrays.asList(array1, array2, array3);
    String[][] arrayOfArrays = data.toArray(new String[0][0]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.