【问题标题】:How to find the index of the Object(that has its own parameters) in arraylist with using indexOf method如何使用 indexOf 方法在 arraylist 中查找对象(具有自己的参数)的索引
【发布时间】:2015-05-27 19:36:25
【问题描述】:

我有对象 Song 的 ArrayList。每个Song 都有自己的参数title, composer, duration,。我正在尝试编写将查找titleobject Song 的方法,并将使用此参数为我提供ArrayList 中对象的INDEXsongs 是 ArrayList 的名称

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

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

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

 classname.findSong("help");

【问题讨论】:

    标签: java arraylist collections indexof


    【解决方案1】:

    试试这个代码:

    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 在 cmets 中表示

    如果有超过一首给定标题的歌曲呢?

    代码:

    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;
    }
    

    【讨论】:

    • thnx Matei。我非常接近这个答案。但我不能(((
    • @MaxZoom 我稍微修改了一下,我只是需要休息一下,太多的不间断编码
    • 给定标题的歌曲多于一首的情况如何?
    【解决方案2】:
    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;
    
    }
    

    【讨论】:

      【解决方案3】:

      如果两首歌曲名称相同,您可以为 Song 类重写 equal 函数以返回 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);
          }
      
      }
      

      【讨论】:

        【解决方案4】:
        public int findSong(String searchtitle) {
                String str;
                int index = 0;
                for(Song s : songs){
                    if(s.title.equalsIgnoreCase(searchtitle))
                        return index;
                    index++;
                }
            }
        

        【讨论】:

          【解决方案5】:
          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);
                      }
                  }
          
              }
          
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2018-09-18
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多