在Arraylist中搜索时遇到问题

时间:2013-11-12 02:32:07

标签: java arraylist indexing

我正在为学校做一个程序,要求我在名人堂上添加/删除/编辑/搜索乐队,用户可以添加乐队。我在arraylist hallOfFame中获取乐队的索引时遇到了麻烦。任何人都可以推荐任何解决方案吗?

这是我的HallofFame班级:

import java.util.*;
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){
        boolean running = true;
        while(running == true){
            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")){
                running = false;
            }
        }
    }

    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 editband = scan.nextLine();
    }

    public static void search(){
        System.out.println("What band are you looking for?");
        String searchband = scan.nextLine();
    }

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

这是类Band的代码:

public class Band
{
    //1.Class Variables    
    private String nameOfBand;
    private String[] members;
    private String genre;
    private int numberOfMembers;
    private int numberOfSongs;
    private int numberOfAlbums;
    private boolean isActive;

    //2. Constructors
    public Band(String name, String genre, int numMem, int numSongs, int numAlbs, boolean isActive)
    {

    }

    //3. Methods
    //setters
    public void setName(String newName)
    {
        nameOfBand = newName;
    }

    public void setGenre(String s)
    {
        genre = s;
    }

    public void setNumberOfMembers(int num)
    {
        numberOfMembers = num;
    }

    public void setNumberOfSongs(int numsongs){
        numberOfSongs = numsongs;
    }

    public void setNumberOfAlbums(int numalbs){
        numberOfAlbums = numalbs;
    }

    public void setIsActive(boolean isactive){
        isActive = isactive;
    }

    public String getName()
    {
        return nameOfBand;
    }

    public String getGenre(){
        return genre;
    }

    public int getNumberofMembers(){
        return numberOfMembers;
    }

    public int getNumberofSongs(){
        return numberOfSongs;
    }

    public int getNumberofAlbums(){
        return numberOfAlbums;
    }

    public boolean getIsActive(){
        return isActive;
    }

    @Override public String toString()
    {
        String output = "";
        output += "Name: " + nameOfBand + "\n";
        output += "Genre: " + genre + "\n";
        output += "Number of members: " + numberOfMembers + "\n";
        output += "Number of songs: " + numberOfSongs + "\n";
        output += "Number of albums: " + numberOfAlbums + "\n";
        output += "Is this band active: " + isActive + "\n";

        return output;
    }
}

2 个答案:

答案 0 :(得分:2)

您可以执行以下代码在ArrayList中搜索带名称的Band。

public static void search(){

 System.out.println("What band are you looking for?");
 String searchband = scan.nextLine();

  boolean bandFooud = false;

  for(Band band : hallOfFame)
    {
        if(band.getName().equals(searchband))
        {
            bandFooud = true;// Set the flag to true to indicate the band is found.
            //Make your code to display the band information here.

            break; 
        }
    }

    if(!bandFooud){
        System.out.printf("Band %s is not found.");
    }
}

答案 1 :(得分:0)

也许你想要这样的东西

public static Band search(){
    System.out.println("What band are you looking for?");
    String searchband = scan.nextLine();

    for (Band b : hallOfFame){
        if (searchBand.equals(b.name){
            return b;
            break;
        }
    }
    return null;
}

另外,如果您希望将toString()方法发送到Band并打印出search()信息,请考虑覆盖return void中的Band方法。

public class Band
{
    //1.Class Variables    
    private String nameOfBand;
    private String[] members;
    private String genre;
    private int numberOfMembers;
    private int numberOfSongs;
    private int numberOfAlbums;
    private boolean isActive;

    //2. Constructors
    public Band(String name, String genre, int numMem, int numSongs, 
                                           int numAlbs, boolean isActive)
    {
        nameOfBand = name;
        this.genre = genre;
        numberOfMemembers = numMem;
        numberOfSongs = numSongs;
        numberOfAlbums = numAlbs;
        this.isActive = isActive;
    }

    @Override
    public String toString(){
        return "Name: " + nameOfBand "
               + "Genre: " + genre
               + " Members: " + numOfMembers
               + " Songs: " + numOfSongs
               + " Albums:" + numOfAlbums
               + " Active? " + isActive; 
    }

然后你可以这样做

public static void search(){
    System.out.println("What band are you looking for?");
    String searchband = scan.nextLine();

    for (Band b : hallOfFame){
        if (searchBand.equals(b.name){
            System.out.println(b);
            break;
        }
    }
}