【问题标题】:Search an arraylist for a string and print the string在数组列表中搜索字符串并打印该字符串
【发布时间】:2013-11-26 08:27:03
【问题描述】:

我正在尝试在 arrayList 中搜索用户输入指定的字符串,然后将搜索结果打印到控制台。我知道我应该使用toIndex(),但我不知道如何使用它。

import java.util.*;

public class searchSongs {

    public static void main(String[]args){

        Scanner searchBar = new Scanner(System.in);
        System.out.println("Enter song title");
        String search = searchBar.nextLine().toUpperCase();

            for (int i = 0; i< MP3_catalogue.artist.size(); i++){
                if (MP3_catalogue.artist.contains(search)){
                    int SV = search.indexOf(search);
                    System.out.println(MP3_catalogue.title.get(SV));
                    System.out.println(MP3_catalogue.artist.get(SV));
                    System.out.println(MP3_catalogue.duration.get(SV));
            }
        }

        MP3_catalogue obj = new MP3_catalogue();
    }
}

编辑:主类是 MP3_catalogue,其中包含 arrayLists。不需要对其他 arrayLists 做任何特别的事情,它们具有与艺术家相同的索引值

import java.util.*;

public class MP3_catalogue {
    static String gotoMenu = new String();

//"gotoMenu" is variable used to return to menu after each interaction with the methods.

public static ArrayList<String> title = new ArrayList<String>(); 
public static ArrayList<String> artist = new ArrayList<String>();
public static ArrayList<String> duration = new ArrayList<String>();

//arrayLists for song elements

@SuppressWarnings("resource")
public static void main(String[]args){
    System.out.println("Welcome to your music catalogue. \n \n" + "Menu choices:");
    System.out.println("A to add songs\n" + "D to delete songs\n" + "S to search catalogue\n" + "C to change song\n" + "? to shuffle catalogue\n");

    gotoMenu = "Y"; 
    while (gotoMenu.equals("Y")){
    System.out.println("Enter your choice:"); 

    Scanner userOption = new Scanner(System.in); //scanner to choose menu option
    String choice = userOption.nextLine().toUpperCase();

        switch (choice) {//switch statement used to go to each menu option

        case "A": addSongs.main(args);//executes addSongs
            System.out.println("Would you like to return to menu? Press Y to return, press N to exit program.");//choice to return to menu
            String goback = userOption.nextLine().toUpperCase();
            if(goback.equals("N"))
            {
                gotoMenu = "N";
            }
            break;

        case "D": deleteSongs.main(args);
            System.out.println("Would you like to return to menu? Press Y to return, press N to exit program.");//choice to return to menu
            String returnMenu = userOption.nextLine().toUpperCase();
            if(returnMenu.equals("N"))
            {
            gotoMenu = "N";
            };
            break;

        case "S": searchSongs.main(args);
                    gotoMenu = "N";
            break;
        case "C": System.out.println("Change songs");
                    gotoMenu = "N";
            break;
        case "?": System.out.println("Shuffle time");
                    gotoMenu = "N";
            break;
        default: System.out.println("Doesn't match a menu choice. Type more carefully this time.");
            break;

        }
    }
}

}

【问题讨论】:

  • 使用正则表达式和模式匹配器。
  • ArrayList在哪里?
  • 请添加完整代码。添加 MP3_catalogue 类详细信息
  • search.indexOf(search); 是什么意思?
  • stackoverflow.com/questions/2642589/… 这可能有助于理解contains() 方法的工作原理

标签: java string arraylist


【解决方案1】:

这就够了。不需要for循环..

        if (MP3_catalogue.artist.contains(search)){
                int SV = MP3_catalogue.artist.indexOf(search);
                System.out.println(MP3_catalogue.title.get(SV));
                System.out.println(MP3_catalogue.artist.get(SV));
                System.out.println(MP3_catalogue.duration.get(SV));
        } else {
               System.out.println("not found");
        }

【讨论】:

    【解决方案2】:

    这看起来不对

    int SV = search.indexOf(search);
    

    你想在循环中从 MP3_catalogue 中获取对象

    for (int i = 0; i< MP3_catalogue.artist.size(); i++){
           Artist artist = MP3_catalogue.artist.get (i);
                if (artist.contains(search)){
                    System.out.println(artist);
    }
    

    由于我不知道你的数据结构,我不能说上述方法是否也适用于标题和持续时间。

    【讨论】:

      【解决方案3】:

      我更喜欢这样写,将整个目录封装到单个 bean 中,并有效地访问/使用。

      class MP3Catalogue {
          private String title;
          private String artist;
          private String duration;
      
          public String getTitle() {
              return title;
          }
      
          public String getArtist() {
              return artist;
          }
      
          public String getDuration() {
              return duration;
          }
      }
      
      
      
      public class SearchSongs {
          public static ArrayList<MP3Catalogue> catelogs =  new ArrayList<MP3Catalogue>();
          public static void main(String[] args) {
              Scanner searchBar = new Scanner(System.in);
              System.out.println("Enter song title");
              String search = searchBar.nextLine().toUpperCase();
      
              for (MP3Catalogue cat : catelogs) {
                  if (cat.getArtist().equalsIgnoreCase(search)) {
                      System.out.println(" Title = " + cat.getTitle() +" Duration = " + cat.getDuration());
                  }
              }
          }
      }
      

      【讨论】:

        【解决方案4】:

        你的代码中for循环的用途是什么?

        也许您可以像这样更改代码:

                for (int i = 0; i< MP3_catalogue.artist.size(); i++){
                    if (MP3_catalogue.artist.get(i).equals(search)){
                        int SV = i;
                        System.out.println(MP3_catalogue.title.get(SV));
                        System.out.println(MP3_catalogue.artist.get(SV));
                        System.out.println(MP3_catalogue.duration.get(SV));
                }
        

        【讨论】:

        • 为什么将 i 分配给 SV。这只是糟糕的编码
        • 是的你是对的,SV变量是额外的,我只是改变你的代码来显示for循环的用法。
        猜你喜欢
        • 2013-04-11
        • 2017-11-17
        • 1970-01-01
        • 1970-01-01
        • 2019-09-17
        • 1970-01-01
        • 2017-06-26
        • 2020-09-29
        • 2011-07-04
        相关资源
        最近更新 更多