【发布时间】: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<Customer>。您永远不会将此变量设置为任何新值。