
本文旨在指导开发者如何使用循环结构,结合二维数组的数据,高效地创建并管理Java对象。通过示例代码,详细解释了如何从数组中提取数据,并将其作为参数传递给对象的构造函数,最终将创建的对象存储在列表中,方便后续操作。
在Java开发中,经常需要根据一组数据动态地创建多个对象。当这些数据以数组的形式存在时,使用循环结构可以简化创建对象的过程。本教程将介绍如何使用 for 循环结合二维数组,创建带有参数的对象,并将这些对象存储在 List 中,以便后续操作。
假设我们需要根据员工信息创建一个 Employee 对象。员工信息存储在一个二维数组中,每一行代表一个员工,每一列代表员工的不同属性,例如姓名、职位和入职年份。
首先,定义 Employee 类:
立即学习“Java免费学习笔记(深入)”;
public class Employee {
private String firstName;
private String lastName;
private String employeeStatus;
private String yearOfHire;
public Employee(String firstName, String lastName, String employeeStatus, String yearOfHire) {
this.firstName = firstName;
this.lastName = lastName;
this.employeeStatus = employeeStatus;
this.yearOfHire = yearOfHire;
}
// Getters 和 Setters (省略)
@Override
public String toString() {
return firstName + ", " + lastName + ", " + employeeStatus + ", " + yearOfHire;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}接下来,创建二维数组存储员工信息:
String[][] empArr = {
{"Moe","Jude","Employee","2017"},
{"Noe","Joel","Employee","2019"},
{"Poe","Juce","Employee","2021"}
};现在,使用 for 循环遍历数组,并创建 Employee 对象:
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
String[][] empArr = {
{"Moe","Jude","Employee","2017"},
{"Noe","Joel","Employee","2019"},
{"Poe","Juce","Employee","2021"}
};
List<Employee> employeeInstances = new ArrayList<>();
String fName, lName, emplStatus, hireYear;
for (String[] ary : empArr) {
fName = "N/A"; lName = "N/A";
emplStatus = "N/A"; hireYear = "N/A";
for (int i = 0; i < ary.length; i++) {
fName = !ary[0].isEmpty() ? ary[0] : fName;
lName = !ary[1].isEmpty() ? ary[1] : lName;
emplStatus = !ary[2].isEmpty() ? ary[2] : emplStatus;
hireYear = !ary[3].isEmpty() ? ary[3] : hireYear;
}
employeeInstances.add(new Employee(fName, lName, emplStatus, hireYear));
}
System.out.println("List all employees to Console Window:");
for (Employee empl : employeeInstances) {
System.out.println(empl.toString());
}
System.out.println();
System.out.println("List all employees first and last names "
+ "to Console Window:");
for (Employee empl : employeeInstances) {
System.out.println(empl.getFirstName() + " " + empl.getLastName());
}
}
}这段代码首先创建了一个 ArrayList 来存储 Employee 对象。然后,使用增强型 for 循环遍历 empArr 数组的每一行。在循环内部,从当前行提取员工的姓名、职位和入职年份,并使用这些信息创建一个新的 Employee 对象。最后,将新创建的对象添加到 employeeInstances 列表中。
创建 ArrayList:
List<Employee> employeeInstances = new ArrayList<>();
创建一个 ArrayList 对象 employeeInstances,用于存储创建的 Employee 对象。ArrayList 是一种动态数组,可以根据需要自动调整大小。
外部循环遍历二维数组的行:
for (String[] ary : empArr) {
// ...
}使用增强型 for 循环遍历 empArr 数组的每一行。ary 变量代表当前行的字符串数组。
内部循环遍历每一行的元素:
for (int i = 0; i < ary.length; i++) {
fName = !ary[0].isEmpty() ? ary[0] : fName;
lName = !ary[1].isEmpty() ? ary[1] : lName;
emplStatus = !ary[2].isEmpty() ? ary[2] : emplStatus;
hireYear = !ary[3].isEmpty() ? ary[3] : hireYear;
}这段代码使用三元运算符来避免空指针异常。如果数组中的元素为空字符串,则使用默认值 "N/A"。
创建 Employee 对象并添加到 ArrayList:
employeeInstances.add(new Employee(fName, lName, emplStatus, hireYear));
使用从数组中提取的数据创建一个新的 Employee 对象,并将其添加到 employeeInstances 列表中。
打印List中的所有Employee对象
System.out.println("List all employees to Console Window:");
for (Employee empl : employeeInstances) {
System.out.println(empl.toString());
}
System.out.println();
System.out.println("List all employees first and last names "
+ "to Console Window:");
for (Employee empl : employeeInstances) {
System.out.println(empl.getFirstName() + " " + empl.getLastName());
}打印Employee对象的toString()方法以及firstName和lastName。
本教程介绍了如何使用 for 循环和二维数组创建带参数的 Java 对象,并将这些对象存储在 ArrayList 中。这种方法可以简化动态创建对象的过程,提高代码的可读性和可维护性。通过学习本教程,开发者可以掌握一种常用的对象创建技巧,并在实际项目中灵活应用。
以上就是使用循环创建带参数的对象:Java 教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号