In this exercise, we were asked to make an ArrayList named “sports”. However, we are then asked to print the elements inside the “sport” ArrayList (no “s”). I thought the console will show an error, but turns out it understood that I wanted the elements inside the “sportS” ArrayList.
Can anyone please explain?
public class GeneralizationsD {
public static void main(String[] args) {
ArrayList<String> sports = new ArrayList<String>();
sports.add("Football");
sports.add("Boxing");
for(String sport : sports) {
System.out.println(sport);
}
pay attention to your for loop you create a variable sport that then refers to sports and to print out the array sports you would call it by the variable that you assigned in your for loop.