Hello. As the title states, in my personal project, I have designed a selection sort algorithm to organize times from earliest to latest. In order to store times, and name of a task, I made a multi-dimensional String ArrayList as follows:
private List<List<String>> tasks = new ArrayList<List<String>>();
After doing research, I ended up with my first attempt at a selection sort algorithm as follows:
private void sort() {
int i = 0; int j = 0;
for(List<String> task : tasks) {
for(List<String> task2 : tasks) {
if(Double.parseDouble(task2.get(1)) > Double.parseDouble(task.get(1))) {
tasks.set(i, task);
tasks.set(j, task2);
}
j++;
}
j = 0;
i++;
}
}
With output:
Hello James. Here are your tasks for today -
Name of task: Dinner Time
Start and End times: 17.0-18.0
Name of task: Food Ordering
Start and End times: 8.0-10.0
Name of task: Boardroom Meeting
Start and End times: 11.0-15.0
I have fixed this issue using a different implementation, but I am still curious as to why this did not work with the for-each loop. Please let me know if you need more code for context. Thank you.