Java - 从Arraylist中搜索和删除对象

时间:2017-12-04 09:01:51

标签: java arraylist

所以我有类Customer和类Bank,银行类有将Customer添加到Customers的arraylist的方法,还必须有一个方法来搜索arraylist中的客户并删除它(从arraylist中删除),怎么能我做到了吗?

银行班 -

private String name;
private double balance;

public Customer(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

@Override
public String toString() {
    return "Customer " +
            "name :'" + name + '\'' +
            '}';
}

客户类 -

public static void deleteAccount(){
    System.out.println("Enter the name you want to delete");
    String name = scanner.nextLine();
    scanner.nextLine();
    bank.deleteAccount(name);
}

主要 -

application/json

正如您所看到的,主类具有从用户获取输入类型String的方法,然后使用该输入调用Bank类的deleteAccount方法,但我不知道如何继续执行deleteAccount方法,如何使它工作?

我需要首先检查用户输入是否在Arraylist中的内容,如果存在则将其从那里删除。

我确实知道输入是String而Arraylist是Customer实例,但Customer只在绑定器中获取String名称,所以我可以这样工作吗?

5 个答案:

答案 0 :(得分:2)

我建议使用HashMap对象而不是ArrayList。 HashMap将一组键(在您的案例中为客户名称)映射到一组对象(客户)

HashMap<String,Customer> customers = new HashMap<String,Customer>();

在addCustomer中你可以做到 this.customers.put(name,customer) 在删除帐户中,您可以这样做 this.customers.remove(name)

否则你可以按照Timothy的建议迭代列表,但这更自然,更有效

答案 1 :(得分:1)

遍历列表并检查提供的名称是否等于从列表中的客户检索到的名称,如果是,则删除它。

public void deleteAccount(String name){

      Iterator<Customer> itr=  customers.iterator();
      while(itr.hasNext()){

          String cuName = itr.next().getName();
          if(cuName.equalsIgnoreCase(name)){
              //delete it from list
              //break out of loop
              itr.remove();
              break;
          }
      }

    }

答案 2 :(得分:1)

按名称删除帐户不正确。因为有多个客户同名的机会。因此,删除帐号将是正确的选项。

您仍然希望按客户名称删除帐户,然后检查以下内容:

Customer custToDelete = null;
for(Customer customer:customers){
        if(customer.getName().equals(name))
        custToDelete = customer;
}

if(custToDelete==null)
    System.out.println("No customer found");
else
    customers.remove(custToDelete);

答案 3 :(得分:1)

我更喜欢使用Map,因此delete / exists将在O(1)而不是O(n)

public class Bank {
private Map<String,Customer> customers = new HashMap<>();

public void addCustomer(String name){

    Customer customer = new Customer(name);
    customers.put(customer.getName(),customer);
    System.out.println("new customer " + customer.getName() + " added");
}

// O(1) -- no need to iterate over an array for deleting customer
public boolean deleteAccount(String name){
    if(customers.containsKey(name)){
        customers.remove(name);
        return true;
    }
    return false;
}

@Override
public String toString() {
    return "Bank{" +
            "customers=" + customers +
            '}';
}

public static void main(String[] args){

    Bank bank = new Bank();                 // create Bank object

    bank.addCustomer("Jhon");       // insert some customer
    bank.addCustomer("Doe");
    System.out.println(bank);
    bank.deleteAccount("Yossi");    // not exists - return false
    bank.deleteAccount("Doe");      // removed - return true

    System.out.println(bank);

}

}

稍微提一下......

答案 4 :(得分:0)

删除Java 8:

  Optional<Customer> customerToDelete = customers.stream().filter(cust->cust.getName().equals(name)).findFirst();

  if(customerToDelete.isPresent()){
    customers.remove(customers);
  }else{
    System.out.println("Customer does not exist");
  }

顺便说一下。我想你应该查看&#34; addCustomer&#34;如果已存在具有给定名称的客户...

之类的东西
 customers.stream().filter(cust->cust.getName().equals(name)).count()

应该做的伎俩