我应该创建一个方法,该方法接受客户的ID,然后如果找到该ID,则返回数组中的索引。该方法只接受整数ID。
我使用迭代二进制搜索来搜索我的客户数组列表。但问题是,在将客户数组与整数进行比较时,它会陷入操作数类型的困境。我曾尝试将方法类型更改为static、CustomerList、Customer等,但这根本不会影响它。
代码中的cl是我在课程开始时创建的公共字段。
作为--
**public class Person {
private String firstName;
private String lastName;
private String address;
private String city;
private String state;
private String zipCode;
public Person(String firstName, String lastName, String address, String city, String state, String zipCode) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.city = city;
this.state = state;
this.zipCode = zipCode;
}
protected Person() {
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getZipCode() {
return zipCode;
}
public void setZipCode(String zipCode) {
this.zipCode = zipCode;
}
@Override
public String toString() {
return "FirstName: " + firstName + "\nLastName: " + lastName + "\nAddress: " + address + "\nCity: " + city + "\nState: " + state + "\nZipCode: " + zipCode;
}
public String toCSV() {
return this.firstName + "," + this.lastName + "," + this.address + "," + this.city
+ "," + this.state + "," + this.zipCode;
}
public void copy(Person p) {
firstName = p.firstName;
lastName = p.lastName;
address = p.address;
city = p.city;
state = p.state;
zipCode = p.zipCode;
}
public void copy(String firstName, String lastName, String address, String city, String state, String zipCode) {
this.firstName = firstName;
this.lastName = lastName;
this.address = address;
this.city = city;
this.zipCode = zipCode;
}
@Override
public Person clone() {
Person p = new Person(this.firstName, this.lastName, this.address, this.city, this.state, this.zipCode);
return p;
}
}**
**public class Customer extends Person{
private int customerID;
private double grossSales;
public Customer(int customerID, double grossSales, String firstName, String lastName, String address, String city, String state, String zipCode) {
super(firstName, lastName, address, city, state, zipCode);
this.customerID = customerID;
this.grossSales = grossSales;
}
public Customer(String s, int customerID, double grossSales, String firstName, String lastName, String address, String city, String state, String zipCode) {
super(firstName, lastName, address, city, state, zipCode);
copyCSV(s);
}
protected Customer() {
}
public int getCustomerID() {
return customerID;
}
public void setCustomerID(int customerID) {
this.customerID = customerID;
}
public double getGrossSales() {
return grossSales;
}
public void setGrossSales(double grossSales) {
this.grossSales = grossSales;
}
@Override
public String toString() {
return "CustomerID: " + customerID + "\nGrossSales: " + grossSales + super.toString();
}
public String toCSV() {
return this.customerID + "," + this.grossSales + "," + super.toCSV();
}
public void copy(Customer c) {
super.copy(c);
customerID = c.customerID;
grossSales = c.grossSales;
}
public void copy(int customerId, double grossSales, String firstName, String lastName, String address, String city, String state, String zipCode) {
super.copy(firstName, lastName, address, city, state, zipCode);
this.customerID = customerId;
this.grossSales = grossSales;
}
public Customer clone() {
Customer c = new Customer(this.customerID, this.grossSales, this.getFirstName(), this.getLastName(), this.getAddress(), this.getCity(), this.getState(), this.getZipCode());
return c;
}
public int compareTo(Customer c) {
int returnValue = 0;
if (this.customerID > c.customerID) {
returnValue = -1;
} else if (this.customerID < c.customerID) {
returnValue = 1;
} else {
returnValue = 0;
}
return returnValue;
}
public void copyCSV(String s) {
List<String> list = new ArrayList<>();
String[] a = s.split(",");
list = Arrays.asList(a);
this.copy(Integer.parseInt(list.get(0)), Double.parseDouble(list.get(1)), list.get(2),
list.get(3), list.get(4), list.get(5), list.get(6), list.get(7));
}
}**
public int indexOf(Integer id) {
int min = 0;
int max = cl.length - 1;
while (min <= max) {
int mid = (min + max) / 2;
if (cl[mid] < id) {
min = mid + 1;
} else if (cl[mid] > id) {
max = mid - 1;
} else {
return mid; // target found
}
}
return -(min + 1); // target not found
}
它应该接受一个CusterID,如果找到则返回数组中的索引,如果没有则插入点。
本质上,我们有一个CSV文件,其中有一个客户列表,每个客户都被分配了一个ID。我们有一个person类和customer类,我包括在其中,我们获取和设置所有变量。以及toString和toCSV方法。在最后一个类CustomerList中,有几个方法。特别是这个,应该接受一个整数,如1、2、3、4等。如果有一个人被分配到该ID号,那么它将返回创建的数组中该人的索引。
但它正陷入if和else-if比较语句中。
假设客户类别,例如:
public class Customer {
private int id;
private String name;
public Customer() {
}
public Customer(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
您可以通过传递以下两者来返回静态方法中客户数组中ID的索引:
public static int indexOf(Customer[] arr, Customer cust) {
int min = 0;
int max = arr.length - 1;
while (min <= max) {
int mid = (min + max) / 2;
if (arr[mid].getId() < cust.getId()) {
min = mid + 1;
} else if (arr[mid].getId() > cust.getId()) {
max = mid - 1;
} else {
return mid; // target found
}
}
return -(min + 1); // target not found
}
您还可以将其转换为非静态方法,并为Customer[]对象创建一个类。或者您可以在main(或任何其他方法)中使用它,如下所示:
Customer cl1 = new Customer(1234,"John");
Customer cl2 = new Customer(1235,"Khal");
Customer cl3 = new Customer(1236,"John");
Customer[] clArray = {cl1,cl2,cl3};
Customer testCustomer = new Customer(1236,"AnyName");
int index = indexOf(clArray, testCustomer);
System.out.println(index); //Prints 2, which is the location of 1236 in the array
客户当前正在存储一个名称和一个ID,并且可以更改为存储您想要的任何值,您只需要编辑构造函数和私有类变量。
此外,如果您打算保存信息并经常更改它,我建议查看ArrayList。数组不是最好处理的事情。
编辑:这是根据您的评论CustomerArray类应该喜欢的:
public class CustomerArray {
private Customer[] cl;
public CustomerArray() {
}
public CustomerArray(Customer...customers ) {
this.cl = customers;
}
public Customer[] getCl() {
return cl;
}
public void setCl(Customer[] cl) {
this.cl = cl;
}
public int indexOf(int id) {
int min = 0;
int max = cl.length - 1;
while (min <= max) {
int mid = (min + max) / 2;
if (cl[mid].getId() < id) {
min = mid + 1;
} else if (cl[mid].getId() > id) {
max = mid - 1;
} else {
return mid; // target found
}
}
return -(min + 1); // target not found
}
}
然后您可以在main方法(或任何其他)中使用它,如下所示:
Customer cl1 = new Customer(1234,"John");
Customer cl2 = new Customer(1235,"Khal");
Customer cl3 = new Customer(1236,"John");
CustomerArray clArray = new CustomerArray(cl1,cl2,cl3);
Customer testCustomer = new Customer(1236,"AnyName");
int index = clArray.indexOf(testCustomer.getId());
System.out.println(index);
所以基本上你是在声明一个CustomerArray类,它包含了你所有的客户。
今天在编程课上我们从streams和Lambda开始,我要做一个练习。 第一个任务是编写一个方法,计算列表中偶数的平均值。我已经编写了这段代码,但显然我没有使用流。 编辑解决方案: 第二个任务是编写一个方法,选择所有以“a”(不区分大小写)开头或有3个字符的字符串,并将其更改为大写。我的代码: 编辑:解决方案: 最后一个任务是返回列表,但将“g”添加到偶数,将“u”添加到奇数。我的代码: 编辑:解
-我创建了一个自定义帖子类型。 > 我把has_archive设为true,也可以设为has_archive= 我还知道,如果我将一个模板文件命名为archive-cpt.php,Wordpress会将这个模板用于我的cpt。 这在某些情况下是可以的。但我希望我的客户能够进入编辑页面,如管理区域中的默认帖子和页面。 我曾考虑过将archive-cpt.php作为一个可以在默认页面中调用的模板。使该
问题内容: 我需要使用python编辑Excel工作簿。有没有这样做的方法,而无需阅读工作簿,编辑我想要的内容并将其写回?即有没有一种方法可以即时进行,因为我只需要在每张纸上编辑几个值? 我已经看过了,和,但他们似乎只支持(据我可以工作),阅读和写作不是编辑。我无法使用,因为我正在使用linux。 对库或特定的工作方式有何建议? 问题答案: xlutils有一个复制模块,可能会与您交错
我试图使用Netty构建一个简单的TCP客户端-服务器应用程序。开始时,我通过SocketChannel从客户端发送消息,方式如下: 所有的消息都被服务器收到了,但是当我想把响应写回客户端时,我发现为了得到客户端的响应,它需要通过引导程序发送消息,并定义将读取响应的Inoundhandler(也许有人知道另一种方法?)当我试图通过引导程序发送消息时,我使用以下代码: 但是通过这种方式,服务器根本没
问题内容: 我正在使用具有浏览器客户端的分布式应用程序中的服务器,并且还参与了与第三方的服务器到服务器通信。我的服务器具有CA签名的证书,可让我的客户端使用HTTP / S和XMPP(安全)通过TLS(SSL)通信进行连接。一切都很好。 现在,我需要通过HTTPS / SSL使用JAX-WS安全地连接到第三方服务器。在此通信中,我的服务器在JAX-WS交互中充当客户端,并且我有一个由第三方签名的客
我正在一个分布式应用程序中的服务器上工作,该应用程序有浏览器客户端,还参与与第三方的服务器到服务器通信。我的服务器有一个CA签名的证书,允许我的客户端使用HTTP/S和XMPP(安全)进行TLS(SSL)通信连接。一切都很好。 现在,我需要通过HTTPS/SSL使用JAX-WS安全地连接到第三方服务器。在这种通信中,我的服务器充当JAX-WS交互中的客户机,我有一个由第三方签署的客户机证书。 我试