包第10章;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Customer {
private String name;
private String streetAddress;
private String phoneNumber;
private int total;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getStreetAddress(){
return streetAddress;
}
public void setStreetAddress(String streetAddress) {
this.streetAddress = streetAddress;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public int getTotal(){
return total;
}
public void setTotal(int total){
this.total = total;
}
public static void assign(){
int a = (int) (Math.random() + 10);
int r = (int) (Math.random() + 10);
int f = (int) (Math.random() + 10);
System.out.println(a + r + f + "x" + "x" + "x");
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
ArrayList <Customer > customerList = new ArrayList <Customer>();
char ans;
do
{
Customer customer = new Customer();
System.out.print("Customer name ");
customer.setName(in.next());
int i = 0;
++i;
System.out.print("Street Address ");
customer.setStreetAddress(in.next());
System.out.print("Phone Number ");
customer.setPhoneNumber(in.next());
customerList.add(customer);
System.out.println("Enter total sales ");
customer.setTotal(in.nextInt());
System.out.println("Would you like to enter in a new customer ( y/n)? ");
String answer = in.next();
ans = answer.charAt(0);
((String) customerList).concat("")
} while(ans == 'y');
for(Customer c: customerList){
System.out.print(c.getName() + "\n" + "Phone number is " +c .getPhoneNumber() +"\n" + "Total sales is "+ c.getTotal() + "\n" + "Address is"+ c.getStreetAddress());
}
for(int i=0; i<customerList.size(); i++){
//System.out.print(customerList.get(i).getName());
}
}
}
我需要为arraylist中的每个值分配一个数字,但是我得到一个错误,表示我必须转换为字符串(arraylist)。我该如何添加它?
答案 0 :(得分:1)
如果我从评论中收集的内容是正确的,那么我相信这就是你想要的:
如果你想要随机值1-10,你当前的assign()是不正确的,它应该是这样的:
public String assign(){
Random rand = new Random();
int a = rand.nextInt(10) + 1;
int r = rand.nextInt(10) + 1;
int f = rand.nextInt(10) + 1;
return a+r+f+"xxx";
}
客户将如下所示:
public class Customer {
private String name;
private String customerNumber;
private String streetAddress;
private String phoneNumber;
private int total;
...
...
...
public String getCustomerNumber() { return this.customerNumber; }
public void setCustomerNumber(String cNumber) { this.customerNumber = cNumber; }
分配数字应如下所示:
for(Customer c : customerList) {
c.setCustomerNumber(assign());
}
还要避免这行代码,这是一个非常糟糕的主意:
((String) customerList).concat("")
您应该将customerNumber重命名为customerID。
答案 1 :(得分:1)
Hiiii
据我所知,你试图为arrayList中的每个值添加数字,同时你要创建customer对象的arrayList,所以首先要了解对象的arrayList,
Customer c1 = new Customer();
Customer c2 = new Customer();
ArrayList<Customer> al = new ArrayList();
al.add(c1);
al.add(c2);
此处此ArrayList对象仅保存customer对象的地址,因此如何更改Customer对象的地址;您不能在Customer类型ArrayList Object中添加数字, 还有另一种方法是将您的ArrayList强制转换为Object类型,然后再不需要进行类型转换,您可以在ArrayList中添加任何类型的对象 像这样,
Customer c1 = new Customer();
Customer c2 = new Customer();
ArrayList<Object> al = new ArrayList();
al.add(c1);
al.add(c2);
答案 2 :(得分:0)
在您的代码中,出现以下行:
((String) customerList).concat("")
尝试将List强制转换为String是注定要失败的 - 我不确定为什么你认为它应该有效。
如果您想要列表的字符串表示,您应该在类Customer
中实现toString()方法,然后您可以执行以下操作:
System.out.println(Arrays.toString(customerList.toArray()));
答案 3 :(得分:0)
您可以使用Map,而不是使用ArrayList。您可以在其中将数字作为密钥和值作为客户。
http://examples.javacodegeeks.com/java-basics/java-map-example/包含使用Map
的示例Storing a new object as the value of a hashmap?中的答案包含有关如何在HashMap中使用Object作为值的信息