I tried using the if-else statement as a condition to add elements to an array list. However, it kept adding one more element above the condition. Kindly review code:
private ArrayList<Musician> songMusicians;
this.songMusicians = new ArrayList<>();
Musician musician = new Musician(name);
if (songMusicians.size() < 6) { //same output for (songMusicians.size() <= 5)
songMusicians.add(musician);
} else {
System.out.println("You can't have more than 5 musicians per song");
}
So instead of adding 5 elements to the array list songMusicians, it added 6 elements. Kindly review code. All contributions would be appreciated.
Aucun commentaire:
Enregistrer un commentaire