搜索arraylist的错误

时间:2014-03-29 17:41:45

标签: arrays search arraylist

我的工作程序创建了一个地址数组,然后搜索它们。

但我的搜索仅查看第二个条目(索引1)如何让我的搜索查看所有条目而不是第二个条目?

随时问我是否还没有发布足够的详细信息!

    public static void main(String[] args)          //start method main
    {
    ArrayList<SJAddressBook> SJ = new ArrayList<SJAddressBook>();
    SJAddressBook aBook = null;
    for (int count =0;count <3;count ++)
    {

    aBook = new SJAddressBook();
    SJ.add(aBook.addEntry());
    System.out.println();
    }
    int foundIndex = aBook.search(SJ);          //execute search
    System.out.println();
    if (foundIndex > -1)
        SJ.get(foundIndex).display();
    else
        System.out.println("No Entry Found!");

我的搜索

    int search(List<SJAddressBook> addressBook)             
    {
    Scanner entry = new Scanner(System.in);

    System.out.println(" Search Menu;\n 1. First Name \n 2. Last Name \n 3. Street Address \n 4. City, State \n 5. Zip Code ");
    System.out.println();
    System.out.print("Please enter Field to Search: ");
    String menu = entry.next();
    System.out.println();
    System.out.print("Please enter Value to Search: ");
    String value = entry.next();
    int index = 0;
    for (SJAddressBook address : addressBook)           //recieve ArrayList as argument
    {
        switch (index)                                  //intitialze switch
        {
            case 1:
            if (address.getFirstName().equalsIgnoreCase(value))
                return index;
                break;
            case 2:
            if (address.getLastName().equalsIgnoreCase(value))
                return index;
                break;
            case 3:
            if (address.getStreetAddress().equalsIgnoreCase(value))
                return index;
                break;
            case 4:
            if (address.getCityState().equalsIgnoreCase(value))
                return index;
                break;
            case 5:
            if (address.getZipCode().equalsIgnoreCase(value))
                return index;
                break;
        }                                               //end switch

        index++;

    }
    return -1;
}

1 个答案:

答案 0 :(得分:0)

我假设你的功能搜索试图在某些字段中找到与用户输入相匹配的书,如果这是正确的,你为什么要使用这个开关?您只需检查字段即可简化代码:

int search(List<SJAddressBook> addressBook) {
    Scanner entry = new Scanner(System.in);

    System.out
            .println(" Search Menu;\n 1. First Name \n 2. Last Name \n 3. Street Address \n 4. City, State \n 5. Zip Code ");
    System.out.println();
    System.out.print("Please enter Field to Search: ");
    String menu = entry.next();
    System.out.println();
    System.out.print("Please enter Value to Search: ");
    String value = entry.next();
    int index = 0;
    for (SJAddressBook address : addressBook) // recieve ArrayList as
                                                // argument
        if (address.getFirstName().equalsIgnoreCase(value) || 
                address.getLastName().equalsIgnoreCase(value) || 
                address.getStreetAddress().equalsIgnoreCase(value) || 
                address.getCityState().equalsIgnoreCase(value) || 
                address.getZipCode().equalsIgnoreCase(value)){
                return index;
        }
        index ++;
    }

    return -1;
}

修改 你的开关功能(这里没用,顺便说一下):

int search(List<SJAddressBook> addressBook) {
    Scanner entry = new Scanner(System.in);

    System.out
            .println(" Search Menu;\n 1. First Name \n 2. Last Name \n 3. Street Address \n 4. City, State \n 5. Zip Code ");
    System.out.println();
    System.out.print("Please enter Field to Search: ");
    String menu = entry.next();
    System.out.println();
    System.out.print("Please enter Value to Search: ");
    String value = entry.next();
    int index = 0;
    for (SJAddressBook address : addressBook) // recieve ArrayList as
        for(int i =0 ; i <= 5; i++){

          switch (index)                                  //intitialze switch
            {
                case 1:
                if (address.getFirstName().equalsIgnoreCase(value))
                    return index;
                    break;
                case 2:
                if (address.getLastName().equalsIgnoreCase(value))
                    return index;
                    break;
                case 3:
                if (address.getStreetAddress().equalsIgnoreCase(value))
                    return index;
                    break;
                case 4:
                if (address.getCityState().equalsIgnoreCase(value))
                    return index;
                    break;
                case 5:
                if (address.getZipCode().equalsIgnoreCase(value))
                    return index;
                    break;
            }   
        }

        index ++;
    }
    return -1;
}