【发布时间】:2021-11-21 18:11:50
【问题描述】:
**当我尝试迭代 ArrayList 中的多个值时,我无法做我能做的事,请帮助我
这里是客户类**
public class customer {
private String CustomerName;
private String phoneNumber;
private String customerId;
public customer(String customerName, String phoneNumber, String customerId) {
this.CustomerName = customerName;
this.phoneNumber = phoneNumber;
this.customerId = customerId;
}
public String getCustomerName() {
return CustomerName;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getCustomerId() {
return customerId;
}
public customer addCustomer(String customerName,String customerId,String phoneNumber)
{
return new customer(customerName, phoneNumber, customerId);
}
}
这是我的主要方法
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Main {
static Scanner scan = new Scanner(System.in);
static ArrayList<customer> customers = new ArrayList<>();
static customer customer = new
customer("dinesh","9600064079","rd_3033");
static Iterator<customer> iterate = customers.iterator();
public static void main(String[] args)
{
System.out.println(customer.getCustomerId());
addCustomer();
printCustomer();
}
public static void addCustomer(){
System.out.println("enter the customer name : ");
String customerName = scan.nextLine();
System.out.println("enter the customer ID : ");
String customerId = scan.nextLine();
System.out.println("enter the customerNumber : ");
String customerNumber = scan.nextLine();
customer addCustomer =
customer.addCustomer(customerName,customerId,customerNumber);
customers.add(addCustomer);
}
public static void printCustomer()
{
//
while(iterate.hasNext()){
customer element = iterate.next();
System.out.println(customers.indexOf(element));
}
}
}
如何在ArrayList中迭代多元素,我该怎么办?
我可以在 arraylist 中使用单个元素,但如何在 arraylist 中迭代多个值
【问题讨论】:
-
到目前为止你做了什么?
-
我尝试对存储在 customer(arraylist) 中的元素进行交互,如果它是单个值,我们可以轻松地对其进行迭代,但添加三个元素,如 customernam、id、number 这就是我被卡住的原因
-
我的问题很简单,如果数组列表在每个索引处都有多个值,例如数组列表在索引号:1 处的颜色名称是否为红色,如何迭代数组列表,如果它在与 index:1->red,blue 相同的行
标签: java arrays arraylist iterator