Unless I miss-read this post (which could be likely), you can do it this way. Read the comments in code:
The Employee Class (you didn't post yours):
public class Employee {
private String firstName;
private String lastName;
private String employeeStatus;
private String yearOfHire;
// An Employee class Constructor specific to the desired need:
public Employee(String firstName, String lastName,
String employeeStatus, String yearOfHire) {
this.firstName = firstName;
this.lastName = lastName;
this.employeeStatus = employeeStatus;
this.yearOfHire = yearOfHire;
}
//GETTERS & SETTERS
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmployeeStatus() {
return employeeStatus;
}
public void setEmployeeStatus(String employeeStatus) {
this.employeeStatus = employeeStatus;
}
public String getYearOfHire() {
return yearOfHire;
}
public void setYearOfHire(String yearOfHire) {
this.yearOfHire = yearOfHire;
}
@Override
public String toString() {
return firstName + ", " + lastName + ", " + employeeStatus + ", " + yearOfHire;
}
}
The code to use the above Employee class:
String[][] empArr = {
{"Moe","Jude","Employee","2017"},
{"Noe","Joel","Employee","2019"},
{"Poe","Juce","Employee","2021"}
};
// Declare a List to hold instances of Employee:
List<Employee> employeeInstances = new ArrayList<>();
/* Variables to be filled from 2D Array and passed
to Employee constructor: */
String fName, lName, emplStatus, hireYear;
// Iterate 2D Array Rows:
for (String[] ary : empArr) {
// Default string variables with "N/A" (Not Available):
fName = "N/A"; lName = "N/A";
emplStatus = "N/A"; hireYear = "N/A";
/* Iterate through the current inner Array (row) and fill
above variables to use in Employee constructor: */
for (int i = 0; i < ary.length; i++) {
/* Use 'Ternary Operator' to handle Array elements
that are Null String (""): */
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;
}
// Create and Add an instance of Employee to the employeeInstances List:
employeeInstances.add(new Employee(fName, lName, emplStatus, hireYear));
}
/* Do whatever you want with the instances of Employee contained
within the employeeInstances List, for example: */
System.out.println("List all employees to Console Window:");
// Iterate through the employeeInstances List:
for (Employee empl : employeeInstances) {
// Display data for current Employee instance:
System.out.println(empl.toString());
}
System.out.println();
System.out.println("List all employees first and last names "
+ "to Console Window:");
// Iterate through the employeeInstances List:
for (Employee empl : employeeInstances) {
/* Display the first and last name of each Employee instance
contained within the employeeInstances List. Employee class
Getter methods are used to get the First and Last names from
the current instance: */
System.out.println(empl.getFirstName() + " " + empl.getLastName());
}
Console Window should display:
List all employees to Console Window:
Moe, Jude, Employee, 2017
Noe, Joel, Employee, 2019
Poe, Juce, Employee, 2021
List all employees first and last names to Console Window:
Moe Jude
Noe Joel
Poe Juce