Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Iterating over a List of Strings In Java?

I am using OpenCSV to read data from a CSV file and am using some of the sample code from the homepage:

CSVReader reader = new CSVReader(new FileReader("stockInfo.csv"));
List myEntries = reader.readAll();

And i am now trying to loop through this list and print out each entry. But i cannot seem to figure out the code to perform this.

Could anyone explain to me how i am supposed to do this becuase i just cant seem to work it out.

like image 621
Elliot Smith Avatar asked Oct 26 '10 21:10

Elliot Smith


People also ask

Can you iterate over strings in Java?

In this approach, we convert string to a character array using String. toCharArray() method. Then iterate the character array using for loop or for-each loop.

Can you iterate through a list in Java?

forEach() Since Java 8, we can use the forEach() method to iterate over the elements of a list. This method is defined in the Iterable interface, and can accept Lambda expressions as a parameter.

How do I traverse a list in Java 8?

In java 8 you can use List. forEach() method with lambda expression to iterate over a list.


1 Answers

Assuming you want to output each entry of each line to it's own line:

    List<String[]> myEntries = reader.readAll();
    for (String[] lineTokens : myEntries) {
        for (String token : lineTokens) {
            System.out.println(token);
        }
    }
like image 81
kaliatech Avatar answered Nov 08 '22 22:11

kaliatech