1

I have the following Code about iterating over a list of elements in a CSV file.

 for (CSVRecord csvRecord : csvParser) {
    // Accessing Values by Column Index
    String name = csvRecord.get(0);

    dates_csv.add(name);
  }

}

How can I start the iteration starting from Index 1 in this for loop. I am still new to java :)

3
  • 3
    Use a regular index-based for loop instead of the current foreach one you're using? Commented Sep 7, 2018 at 12:52
  • Thank you! How to write it? Commented Sep 7, 2018 at 12:53
  • 3
    docs.oracle.com/javase/tutorial/java/nutsandbolts/for.html Commented Sep 7, 2018 at 12:55

2 Answers 2

4
boolean firstRound = true;
for (CSVRecord csvRecord : csvParser) {
    if(firstRound){
        firstRound = false;
    } else {
        String name = csvRecord.get(0);
        dates_csv.add(name);
    }
}

or

List<CSVRecord> recList = csvParser.getRecords();
for(int i = 1; i < recList.size(); i++){
    dates_csv.add(recList.get(i).get(0));
}
Sign up to request clarification or add additional context in comments.

Comments

1
List<CSVRecord> parserList = parser.getRecords();
for (CSVRecord csvRecord : parserList.subList(1, parserList.size()))
{ 
    //code here
} 

3 Comments

How will you get subList from an Iterable?
List<CSVRecord> parserList = parser.getRecords();
for (CSVRecord csvRecord : parserList.subList(1, parserList.size())) { //code here } }

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.