【问题标题】:Arraylist indexOf always returns -1 JavaArraylist indexOf 总是返回 -1 Java
【发布时间】:2013-11-11 23:23:47
【问题描述】:

我正在为班级做一个项目,在这个项目中你必须成为名人堂,并且能够添加/删除/搜索/编辑不同类型的乐队。现在,我在搜索特定波段的索引时遇到问题,因为它总是返回 -1,我不知道为什么。

这是我的代码:

public class HallofFame
{
    public static ArrayList<Band> hallOfFame = new ArrayList<Band>();
    public static Scanner scan = new Scanner(System.in);
    public static void main(String[]args){
        int a = 0;
        while(a == 0){
            System.out.println("What would you like to do?");
            System.out.println("");
            System.out.println("1. Add");
            System.out.println("2. Remove");
            System.out.println("3. Edit");
            System.out.println("4. Clear");
            System.out.println("5. Search");
            System.out.println("6. Quit");
            System.out.println("");
            String choice = scan.nextLine();
            if(choice.equals ("1")){
                add();
            }
            else if(choice.equals ("2")){
                remove();
            }
            else if(choice.equals ("3")){
                edit();
            }
            else if(choice.equals ("4")){
                clear();
            }
            else if(choice.equals ("5")){
                search();
            }
            else if(choice.equals ("6")){
                quit();
                break;
            }
        }
    }

    public static void add(){
        Scanner booblean = new Scanner(System.in);
        System.out.println("What is the name of the band you would like to add?");
        String name = scan.nextLine();
        System.out.println("What kind of genre is this band?");
        String genre = scan.nextLine();
        System.out.println("How many members are in the band?");
        int numMem = scan.nextInt();
        System.out.println("How many songs does this band have?");
        int numSongs = scan.nextInt();
        System.out.println("How many albums does this band have?");
        int numAlbs = scan.nextInt();
        System.out.println("Is this band currently active?");
        String yesno = booblean.nextLine();
        boolean isActive = false;
        if(yesno.equalsIgnoreCase ("yes")){
            isActive = true;
        }
        Band b1 = new Band(name, genre, numMem, numSongs, numAlbs, isActive);
        hallOfFame.add(b1);
        System.out.println("");
        System.out.println("The band " + name + " has been added to the database.");
        System.out.println("");
    }

    public static void remove(){

    }

    public static void edit(){
        System.out.println("What band info do you want to edit?");
        String searchband = scan.nextLine();
    }

    public static void clear(){
        hallOfFame.clear();
    }

    public static void search(){
        System.out.println("What band name are you searching for?");
        String searchband = scan.nextLine();
        int retval = hallOfFame.indexOf(searchband);
        System.out.println("The band " + searchband + " is at index: " + retval);
    }

    public static void quit(){
        System.exit(0);
    }
}

搜索方法是我遇到问题的方法。

【问题讨论】:

  • 请说明您如何填充hallOfFame 以及返回-1 的示例搜索输入。
  • 你能给我们展示一下整个课程,而不仅仅是这一种方法吗?
  • booblean - 认真的吗? :D
  • hallOfFame 是一个ArrayList&lt;Band&gt;,因此您将无法将 indexOf 与字符串一起使用。您必须自己搜索。
  • 关于程序整体执行的注意事项:while(a == 0){ 部分表示只要a 为0,您就在循环。您应该做一些事情,而不是创建int a = 0;喜欢boolean running = true;。然后你去while(running){ 而不是另一个while 条件。现在,在第 6 个选项中,退出选项,而不是调用 quit() 并中断,只需设置 running = false;。这意味着您的程序将正常结束,而不是被杀死。与人类相比,你的程序会老死,而不是早死。

标签: java arraylist indexof


【解决方案1】:

问题是hallOfFame 包含Band 对象,但您正在搜索hallOfFame 以查找String。相反,遍历hallOfFame 并将波段名称与输入的字符串进行比较。

【讨论】:

    【解决方案2】:

    或者,您可以覆盖 Bandequals 方法,以便 indexOf 真正起作用。

    我想它会这样写:

    @Override
    
    public boolean equals(Object o) {
    
      return ((Band) o).name==this.name;
    
    }
    

    【讨论】:

    • 我建议改为返回以下内容:return o instanceof Band && ((Band) o).name==this.name;
    【解决方案3】:

    您应该同时覆盖 equals 和 hashCode 以使其完美运行。

    Overriding equals and hashCode in Java

    【讨论】:

      【解决方案4】:

      一种方法是您需要使用参数为名称创建一个乐队的构造函数。然后你可以在 ArrayList hallOfFame 中搜索对象“Band”

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-26
        • 1970-01-01
        • 2017-04-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多