如何使用 indexOf 方法在 arraylist 中查找对象(具有自己的参数)的索引

How to find the index of the Object(that has its own parameters) in arraylist with using indexOf method

我有对象的 ArrayList Song。每个 Song 都有自己的参数,title, composer, duration,。我正在尝试编写将寻找 object Songtitle 的方法,并将使用此参数给我 ArrayList 中对象的 INDEXsongs是ArrayList的名字

public int findSong(String title) {
        int index = songs.indexOf(title);

        System.out.println(index);
        return index;
}

在这种情况下,方法正在寻找名称为 help 的对象,但是如何编写他将寻找具有参数 index 的对象的方法 title 等于 help。我只是想了解其中的逻辑)

 classname.findSong("help");

如果两首歌曲名称相同,您可以将 Song class 的相等函数覆盖为 return true

public class Song{

    //Your other fields and functions ....

    @Override
    public boolean equals(Object obj) {
       if (!(obj instanceof Song))
            return false;
        if (obj == this)
            return true;

        Song s= (Song) obj;
        return s.getTitle().equals(this.title);
    }

}

试试这个代码:

public int findSong(String title) {
    int index = -1;
    // Iterate over the elements of the list
    for (Song song : songs) {
        if (song.getTitle().equals(title)) index = songs.indexOf(song);
    }
    // If you didn't know here we have if / else
    // if index == -1 print song not found else print the index
    System.out.println(index == -1 ? "Song not found: " + title : "Sound found at index " + index);
    // If song isn't found index is -1
    return index;
}

编辑: Max Zoom 在评论中说

How about the case where there is more then one song with given title?

代码:

public int[] findSong(String title) {
    List<Integer> indexesList = new ArrayList<>();
    // Iterate over the elements of the list
    for (Song song : songs) {
        if (song.getTitle().equals(title)) indexesList.add(songs.indexOf(song));
    }
    // If we have no songs return empty array
    if (indexesList.size() == 0) return new int[0];
    // Convert list to int array
    int[] indexes = new int[indexesList.size()];
    for (int i = 0; i < indexes.length; i++) {
        Integer integer = indexesList.get(i);
        if (integer != null) indexes[i] = integer.intValue();
        else indexes[i] = -1;
    }
    return indexes;
}
public int findSong(String searchtitle) {
        String str;
        int index = 0;
        for(Song s : songs){
            if(s.title.equalsIgnoreCase(searchtitle))
                return index;
            index++;
        }
    }
public int findSong(String title, List<Song> songs) {

    for (Song song : songs) {
      if (song == null || song.getTitle() == null) {
        continue;
      }
      if (song.getTitle().equals(title)) {
       int index = songs.indexOf(song);
       System.out.println(index);
       return index;
      }
    }
    return -1;

}
import java.util.ArrayList;
import java.util.List;

public class SongDriver
{

    public static void main(String[] args)
    {
        Song song1 = new Song();
        Song song2 = new Song();
        Song song3 = new Song();

        song1.setTitle("song1");
        song2.setTitle("help");
        song3.setTitle("song3");

        List<Song> songs = new ArrayList<Song>();
        songs.add(song1);
        songs.add(song2);
        songs.add(song3);

        for (int i = 0; i < songs.size(); i++)
        {
            Song song = songs.get(i);
            if (song.getTitle().equals("help"))
            {
                System.out.println("Help is found at index " + i);
            }
        }

    }

}