Looping a Java List based on a pre-knowledge of its element data?

48 Views Asked by At

Consider the scenario:

List<String> weekdays = Arrays.asList("sun", "mon", "tue");
long sunTemp = 0;
long monTemp = 0;
for(String day : weekdays){
  if(day.equals("sun"){
  sunTemp = getSunTemp();
  }else if(day.equals("mon")){
  monTemp = getMonTemp();
  // use sunTemp ex: totalTemp = sunTemp + monTemp
  }
  //...more code
}

What, if anything is wrong with the above?

The following stand out as awkward to me:
- usage of the for loop
- assigning a value in the 'if block' and use it in the 'else block' the next iteration.
- Code depending on the pre-knowledge of data (order and exact values the list.)

Can someone put in formal terms what is correct or wrong with the above?

Thanks much!

1

There are 1 best solutions below

0
Toxxic On

The brackets are not needed for one liner if/else statements, but other than that it seems fine.