ArrayList搜索始终返回最后一个对象

时间:2015-08-29 18:16:04

标签: java arraylist

我正在尝试创建一个存储联系信息的程序,并具有搜索功能。但无论我搜索什么,它总是返回添加到ArrayList的最后一个对象。我认为这与for循环有关,但我不确定。我是编写代码的新手,我很感激任何输入。

这是我的搜索方法:

    public void searchContacts()
{
    Scanner stdIn = new Scanner (System.in);
    System.out.println("Search Menu;\n1. First Name\n2. Last Name\n3. Street Address\n4. City, State\n5. Zip Code\n");
    System.out.println("Please Enter Field to Search: ");
    search = stdIn.nextInt();
    Scanner stdIn1 = new Scanner (System.in);
    System.out.println("Please enter value to search for: ");
    value = stdIn1.nextLine();

    switch (search)
    {
        case 1:
            for(int i=0; i<addressBook.size();i++)
            {   
                if(value.equals(addressBook.get(i).getFirstName()));
                {
                indexFound = i;
                }                   
            }
            break;
        case 2:
            for(int i=0; i<addressBook.size();i++)
            {
                if(value.equalsIgnoreCase(addressBook.get(i).getLastName()));
                {
                indexFound = i;
                }
            }
            break;
        case 3:
            for(int i=0; i<addressBook.size();i++)
            {
                if(value.equalsIgnoreCase(addressBook.get(i).getStreetAddress()));
                {
                indexFound = i;
                }
            }
            break;
        case 4:
            for(int i=0; i<addressBook.size();i++)
            {
                if(value.equalsIgnoreCase(addressBook.get(i).getCityState()));
                {
                indexFound = i;
                }
            }
            break;
        case 5:
            for(int i=0; i<addressBook.size();i++)
            {
                if(value.equalsIgnoreCase(addressBook.get(i).getZipCode()));
                {
                indexFound = i;
                }
            }
            break;
        default:
            indexFound = -1;
    }       
}

如果需要它是我的显示方法:

public void display()
{
    System.out.println();
    if (indexFound > -1)
    {
    System.out.println("First Name: " + addressBook.get(indexFound).getFirstName());
    System.out.println("Last Name: " + addressBook.get(indexFound).getLastName());
    System.out.println("Street Address: " + addressBook.get(indexFound).getStreetAddress());
    System.out.println("City, State: " + addressBook.get(indexFound).getCityState());
    System.out.println("Zip Code: " + addressBook.get(indexFound).getZipCode());
    }        
    else
    System.out.println("No Entry Found");

}

联系班级

public class Contact 
{
private String firstName;
private String lastName;
private String streetAddress;
private String cityState;
private String zipCode;

public Contact (String first, String last, String street, String cS, String zip)
{
    firstName = first;
    lastName = last;
    streetAddress = street;
    cityState = cS;
    zipCode = zip;
}

public String getFirstName() 
{
    return firstName;
}

public String getLastName() 
{
    return lastName;
}

public String getStreetAddress() 
{
    return streetAddress;
}

public String getCityState() 
{
    return cityState;
}

public String getZipCode() 
{
    return zipCode;
}



}

4 个答案:

答案 0 :(得分:3)

一旦找到第一个元素就应该中断。

if(value.equals(addressBook.get(i).getFirstName()));
            {
              indexFound = i;
              break;
            } 

答案 1 :(得分:2)

问题是你的每个if语句后面都跟着一个分号:

if (value.equals(addressBook.get(i).getFirstName())); // HERE is the problem - the semicolon

......字面上解释为“如果找到,什么都不做”。在评估if语句之后,它总是会跳转到这段代码中:

{
    indexFound = i;
}

因此,如果您在设置if后立即从方法中删除了return语句后面的所有分号和indexFound,那么您应该没问题。

代码看起来与此相似 - 遗憾的是我现在无法对此进行测试......

switch (search)
{
    case 1:
        for (int i=0; i<addressBook.size(); i++)
        {   
            if(value.equals(addressBook.get(i).getFirstName()))
            {
                indexFound = i;
                return;
            }                   
        }
        break;
    case 2:
    // etc.

答案 2 :(得分:1)

你循环不会因成功匹配而中断。它循环到最后一个索引。

根据以下代码段进行更改:

        for(int i=0; i<addressBook.size();i++)
        {
            if(value.equalsIgnoreCase(addressBook.get(i).getZipCode()));
            {
            indexFound = i;
            break;
            }
        }
        break;

根据您的要求,应该重写如下:

indexFound=-1;
for(int i=0; i<addressBook.size() && indexFound==-1 ;i++){
    switch (search)
    {
        case 1:
               if(value.equals(addressBook.get(i).getFirstName()));
                 indexFound = i;
               break;
        case 2:
               if(value.equalsIgnoreCase(addressBook.get(i).getLastName()));
                indexFound = i;
               break;
        case 3:
              if(value.equalsIgnoreCase(addressBook.get(i).getStreetAddress()));
                indexFound = i;
              break;
        case 4:
             if(value.equalsIgnoreCase(addressBook.get(i).getCityState()));
                indexFound = i;
              break;
        case 5:
                if(value.equalsIgnoreCase(addressBook.get(i).getZipCode()));
                indexFound = i;
              break;
        default:
            indexFound = -1;
    }  
}

答案 3 :(得分:1)

每个案例的每个break条件都应该有一个if语句。对于特定的代码,它应该是这样的......

case 1:
        for(int i=0; i<addressBook.size();i++)
        {   
            if(value.equals(addressBook.get(i).getFirstName()));
            {
            indexFound = i;
            break;
            }                   
        }
        break;

在每种情况下都实现这一点。