如果您在Customer 类中有一个二维字符串数组和一个全参数构造函数,那么您可以将一个字符串数组转换为一个对象数组 像这样:
static class Customer {
String name, age, gender;
public Customer(String name, String age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
@Override
public String toString() {
return name + " " + age + " " + gender;
}
}
public static void main(String[] args) {
String[][] arrStr = {
{"John1", "22", "Male"},
{"John2", "21", "Male"},
{"John3", "23", "Male"},
{"John4", "24", "Male"},
{"John5", "20", "Male"}};
Customer[] customers = Arrays.stream(arrStr)
// convert an array of strings to an array of objects
.map(arr -> new Customer(arr[0], arr[1], arr[2]))
.toArray(Customer[]::new);
// output
Arrays.stream(customers).forEach(System.out::println);
}
输出:
John1 22 Male
John2 21 Male
John3 23 Male
John4 24 Male
John5 20 Male