The code is extremely difficult to maintain, and so it has some bugs that you should solve (see below).
But to print the list, I suggest creating a toString() method in the Music class:
@Override
public String toString() {
return "Musica [_titulo=" + _titulo + ", _autor=" + _autor
+ ", _duracao=" + _duracao + ", _ano=" + _ano + ", _genero="
+ _genero + "]";
}
This makes it easier to iterate over the array.
In the listMusic() method call this list and use a Stringbuilder (which is more efficient) for String concatenation:
public String listarMusicas() {
StringBuilder sb = new StringBuilder();
for(Musica m: _listaMusica) {
if(m!=null) {
sb.append(m);
}
}
return sb.toString();
}
Once done, your code still has some issues that need to be fixed. I’ll list them here:
- Do not use an Array for the playlist. Use an
List<Musica>
. This is the correct structure for your case, in which the list can grow and decrease. Another reason is not need to check if the element is null. The List has the exact size you need, and you can use foreach to iterate over it easily.
- Do not use the variable
_musicas
to control the size of the Array (or List). The Array has the length property and the List has a size().
- When removing a song from the list, it does
_musicas--
. Only if you remove several times, the variable turns negative! This generates an Arrayoutofboundsexception. Another reason not to control this from the outside.
- Do not use variables with underscore (_) as it decreases readability.
- Do not use non-standard accents and characters variables (
getTítulo
, getDuração
). This will make it easier for you when opening the code by other editors (or other people using it).
Good luck in your work.
I don’t understand. What do you want to do? The list methodMusicas() already answers your question. Maybe adding an n after getTitulo() is better to read, but I don’t know if that’s what you want.
– igventurelli
Why don’t you use
List<Musica>
?– Victor Stafusa
When I do it it doesn’t really show the list of songs
– Amadeu Antunes
Some can help me?
– Amadeu Antunes