【问题标题】:2D ArrayList Repeating All Entries Java2D ArrayList 重复所有条目 Java
【发布时间】:2021-11-11 20:13:12
【问题描述】:

我正在尝试创建一个 2D ArrayList,但我所做的每个条目都被添加到一个条目中,然后重复。本质上不是得到 [[a, b, c], [d, e, f]] 我得到 [[a, b, c, d, e, f], [a, b, c, d, e , F]]。这是关于我的问题的代码。

public class Customer {
  
  private String customerId;
  private String name;
  private String address;
  private String phoneNumber;
  
  public Customer(String customerId, String name, Address address, String phoneNumber) {
    this.customerId = customerId;
    this.name = name;
    this.address = address.getAddressInfo();
    this.phoneNumber = phoneNumber;
  }

  public String getCustomerId() {
    return customerId;
  }

  public void setCustomerId(String customerId) {
    this.customerId = customerId;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(Address address) {
    this.address = address.getAddressInfo();
  }

  public String getPhoneNumber() {
    return phoneNumber;
  }

  public void setPhoneNumber(String phoneNumber) {
    this.phoneNumber = phoneNumber;
  }

  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + ((address == null) ? 0 : address.hashCode());
    result = prime * result + ((customerId == null) ? 0 : customerId.hashCode());
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    result = prime * result + ((phoneNumber == null) ? 0 : phoneNumber.hashCode());
    return result;
  }

  @Override
  public boolean equals(Object obj) {
    if (this == obj)
      return true;
    if (obj == null)
      return false;
    if (getClass() != obj.getClass())
      return false;
    Customer other = (Customer) obj;
    if (address == null) {
      if (other.address != null)
        return false;
    } else if (!address.equals(other.address))
      return false;
    if (customerId == null) {
      if (other.customerId != null)
        return false;
    } else if (!customerId.equals(other.customerId))
      return false;
    if (name == null) {
      if (other.name != null)
        return false;
    } else if (!name.equals(other.name))
      return false;
    if (phoneNumber == null) {
      if (other.phoneNumber != null)
        return false;
    } else if (!phoneNumber.equals(other.phoneNumber))
      return false;
    return true;
  }
  
  public String toString() {
    return String.format("ID: %s, Name: %s, Address: %s, Phone: %s", 
        customerId, name, address, phoneNumber);
  }
}
import java.util.ArrayList;
import java.util.List;

public class ParkingOffice {
  
  private String name;
  private String address;
  private List<List<Customer>> customers = new ArrayList<>();
  private List<Customer> customerList = new ArrayList<>();
  
  public ParkingOffice(String name, Address officeAddress) {
    this.name = name;
    this.address = officeAddress.getAddressInfo();
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getAddress() {
    return address;
  }

  public void setAddress(Address address) {
    this.address = address.getAddressInfo();
  }

  public List<List<Customer>> getCustomers() {
    return customers;
  }

  public void setCustomers(List<List<Customer>> customers) {
    this.customers = customers;
  }

  public List<Customer> getCustomerList() {
    return customerList;
  }

  public void setCustomerList(List<Customer> customerList) {
    this.customerList = customerList;
  }

  public Customer register(String customerId, String name, Address address, String phone) {
    Customer customer = new Customer(customerId, name, address, phone);
    customer.addCustomers();
    this.customerList.add(customer);
    this.customers.add(this.customerList);
    return customer;
  }

}
import static org.junit.jupiter.api.Assertions.*;

import java.util.ArrayList;
import java.util.List;

import org.junit.jupiter.api.Test;

class ParkingOfficeTest {
  
  Address address = new Address("123 Lane", null, "Anaheim", "CA", "92801");
  Customer customer = new Customer("7g8h9i", "Jane", address, "987654321");
  Customer customer2 = new Customer("4d5e6f", "Joe", address, "987654321");

  @Test
  void testRegisterCustomer() {
    parkingOffice.register(customer.getCustomerId(), customer.getName(), address, customer.getPhoneNumber());
    parkingOffice.register(customer2.getCustomerId(), customer2.getName(), address, customer2.getPhoneNumber());
    List<List<Customer>> customers = parkingOffice.getCustomers();
    assertEquals("[[ID: 7g8h9i, Name: Jane, Address: 123 Lane, Anaheim, CA 92801, Phone: 987654321], [ID: 4d5e6f, Name: Joe, Address: 123 Lane, Anaheim, CA 92801, Phone: 987654321]]", customers);
  }
}
Expected: [[ID: 7g8h9i, Name: Jane, Address: 123 Lane, Anaheim, CA 92801, Phone: 987654321], [ID: 4d5e6f, Name: Joe, Address: 123 Lane, Anaheim, CA 92801, Phone: 987654321]]
Actual: [[ID: 7g8h9i, Name: Jane, Address: 123 Lane, Anaheim, CA 92801, Phone: 987654321, ID: 4d5e6f, Name: Joe, Address: 123 Lane, Anaheim, CA 92801, Phone: 987654321], [ID: 7g8h9i, Name: Jane, Address: 123 Lane, Anaheim, CA 92801, Phone: 987654321, ID: 4d5e6f, Name: Joe, Address: 123 Lane, Anaheim, CA 92801, Phone: 987654321]]

提前感谢您的任何反馈和帮助。

【问题讨论】:

  • 您从未在代码中的任何位置创建新的customerList。您只有在 ParkingOffice 类的初始化程序中创建的初始 List&lt;Customer&gt;。您永远不会将此变量设置为任何新值。

标签: java arraylist


【解决方案1】:

您的ParkingOffice 有一个customerList 属性,它是一个客户对象列表。它还有一个customers 属性,我认为这是您要询问的二维列表。

当您注册客户时,您将其添加到customerList,这是有道理的。但随后您也将该列表 (customerList) 添加到您的二维列表 (customers)。

因此,对于每个新客户,您都向这两个列表中的每一个添加一个新元素:customerList 获取新客户,customers 获取指向 customerList 的新元素。对于customers,您正在创建一个看起来像[ customerList, customerList, ... ] 的数组

目前尚不清楚customers 应该包含什么,但我怀疑您可以根据要求构建它。因此,在getCustomers() 中,您可以返回一个新列表,其中一个元素指向您的customerList

我还想指出,您有一个关于哈希码的细微错误。您正在使用可变属性计算 Customer 中的哈希码。这意味着有人可以创建Customer 的实例,将其放入哈希表(或其他使用哈希码的结构)中,然后修改客户的姓名或地址,从而更改其哈希码。当您尝试在哈希表中找到该客户时,您将无法将其取回,因为其哈希码已更改。

您应该只使用不可变的属性来计算哈希码。

【讨论】:

    猜你喜欢
    • 2013-03-05
    • 2018-10-16
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    • 2014-09-27
    • 2016-03-04
    • 2023-03-20
    相关资源
    最近更新 更多