我无法理解 ArrayList。我正在使用 3 个类客户、视频和发票编写程序。在下面的代码中,我正在创建一个新客户以添加到设置 ArrayList,但我觉得我正在尝试将它视为一个数组。我希望用户能够添加另一个客户对象并使用计数器“i”并运行一系列问题以添加到该客户对象。我意识到其中一些是一团糟。
import java.util.Scanner;
import java.util.ArrayList;
public class Prog4 {
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
ArrayList <Customer> customer = new ArrayList <Customer>();
int i = 0;
char ans;
do
{
System.out.print("Customer name: ");
String name = in.next();
customer.add(i,).setName(name);
System.out.print("Street Address: ");
String streetAddress = in.next();
d1.setStreetAddress(streetAddress);
System.out.print("City: ");
String city = in.next();
d1.setCity(city);
System.out.print("State: ");
String state = in.next();
d1.setState(state);
System.out.print("Zipcode: ");
String zipcode = in.next();
d1.setZipcode(zipcode);
System.out.print("Phone Number: ");
String phoneNumber = in.next();
d1.setPhoneNumber(phoneNumber);
customer[i] = new Customer(name, streetAddress, city, state, zipcode, phoneNumber);
System.out.print("Would you like to enter in a new customer (y/n)? ");
String answer = in.next();
ans = answer.charAt(0);
}while(ans == 'y');
}
}