Reputation: 172
I have a method which takes an ArrayList<Song>
as argument.
the class song has different methods like getTitle(), getInterpret(), toString(), toString2()...
public class Song implements Comparable<Song>{
private String title;
private String interpret;
public Song(String title,String interpret){
this.title=title;
this.interpret=interpret;
}
public String getTitle() {
return title;
}
public String getInterpret() {
return interpret;
}
public String toString2(){
return title+" - "+interpret;
}
@Override
public int compareTo(Song otherSong) {
return title.compareToIgnoreCase(otherSong.getTitle());
}
public String toString() {
return interpret+" - "+title;
}
}
the method printList takes an ArrayList of Song as argument:
public <Song> void printList(ArrayList<Song> songList,String sortBy){
JLabel track;
for(int i=0;i<songList.size();i++){
if(sortBy=="Artist"){
track = new JLabel("\n- "+songList.get(i).toString());}
else if(sortBy=="Song"){
track = new JLabel("\n- "+songList.get(i).toString2());}
}
I have an error on songList.get(i).toString2(), the method toString2() is undefined for type Song, it seems like I can only access to the method of the class Object.
Can somebody help please?
Upvotes: 0
Views: 484
Reputation: 691765
You've made your method generic, and chose to name the generic type Song. It shouldn't be generic:
public void printList(ArrayList<Song> songList,String sortBy){
Also, compare strings with equals()
, not ==
.
Upvotes: 3