I am getting data from a csv. If a row in csv doesn't contain values, I will need to update it with the previous row values. The way I am doing it is -
- Parse the csv and add all the rows to a linked list.
- Iterate through the list and check if a row is empty then fill it with the previous row.
Point #2 is not working as expected. Below is my code snippet:
ListIterator<Data> li = dataList.listIterator(0);
while (li.hasNext()) {
Data prev = null;
if (li.hasPrevious()) {
prev = li.previous();
System.out.println("Previous Node is: "+prev);
}
Data data = li.next();
if (data.getNumber() == null || data.getNumber().trim().isEmpty()) {
data.setPartType(prev.getPartType());
data.setNumber(prev.getNumber());
}
I believe its happening because when we call next() it moves the pointer to the next and hence the previous element will be the current element.
Question - how do I get the actual previous element?