你好我对我试图解决的一些编码问题感到困惑。
我有一些字符串数组:
String[] firstNames= {"Fred","John","Amir", "James","Bob","Jay","Amber"};
String[] lastNames = {"Bond","Kates","Memar", "White","Marley","Brown","Nogofski"};
String[] idNumbers = {"R111111","A222222","AB11111", "KR22121","V311133","L242434","P102432"};
String[] employeeNum = {"1111","2222","3333", "4444","5555","6666","7777"};
我必须创建一个数组,并以某种方式组织上面方法Employee[] list = new Employee[firstNames.length];
list = listOfEmployees(firstNames,lastNames,idNumbers); // create the list of employees in one array
我开始写出方法:
public static Employee[] listOfEmployees(String[] firstName, String[]
lastName, String[] idNumber){
}
但不确定如何处理此问题。也不确定我的参数是否正确。
最终结果应该是这样的:
Employee #1
first name:Fred Last Name:Bond
Id number:R111111
。 。
Employee #2
first name:John Last Name:Kates
Id number:A222222
依旧......
提前感谢。
修改:
员工类:
public class Employee{
private String firstName;
private String lastName;
private String idNumber;
private String employeeNumber;
private int employeeCount;
/**
* Constructor
* @param firstName first name
* @param lastName last name
* @param idNumber id number
*/
public Employee(String firstName, String lastName, String idNumber){
this.firstName = firstName;
this.lastName = lastName;
this.idNumber = idNumber;
employeeCount = 0;
}
/**
* Accessors here
*/
public String getFirstName(){
return firstName;
}
public String getLastName(){
return lastName;
}
public String getIdNumber(){
return idNumber;
}
public String getEmployeeNumber(){
return employeeNumber;
}
// mutators here
/**
* @param firstName first name
*/
public void setFirstName(String firstName){
this.firstName = firstName;
}
/**
* @param lastName last name
*/
public void setLastName(String lastName){
this.lastName = lastName;
}
/**
* @param idNumber id number
*/
public void setIdNumber(String idNumber){
this.idNumber = idNumber;
}
/**
* @param employeeNumber employee number
*/
public void setEmployeeNumber(String employeeNumber){
this.employeeNumber = "";
}
@Override
public String toString(){
String result = "First name: " + getFirstName() + "\nLast name: " + getLastName()
+ "\nId number: " + getIdNumber() + "\nEmployee number: ";
if(getEmployeeNumber() == null){
return result + "No employee number has been assigned yet!";
}
return result + getEmployeeNumber();
}
}
答案 0 :(得分:3)
请尝试以下方法:
private static Employee[] listOfEmployees(String[] firstNames, String[] lastNames, String[] idNumbers){
Employee[] list = new Employee[firstNames.length];
for(int i=0; i<list.length; i++){
list[i]=new Employee(firstNames[i], lastNames[i], idNumbers[i]);
}
return list;
}
要打印上述功能返回的数组,您可以使用:
private static void printEmployees(Employee[] employees){
for (Employee employee : employees) {
System.out.println("ID: "+employee.getIdNumber());
System.out.println("Name : "+employee.getFirstName()+" "+employee.getLastName());
System.out.println("------------------------------------");
}
}
按照以下声明打电话给他们:
printEmployees(listOfEmployees(firstNames,lastNames,idNumbers));
答案 1 :(得分:0)
执行for循环并使用Employee构造函数初始化对象:
Employee[] list = new Employee[firstNames.length];
for (int i = 0; i < firstName.length; i++) {
list[i] = new Employee(firstName[i], lastName[i] ...
}
答案 2 :(得分:0)
试试这个
public class Test{
public static void main(String[] args){
String[] firstNames= {"Fred","John","Amir", "James","Bob","Jay","Amber"};
String[] lastNames = {"Bond","Kates","Memar", "White","Marley","Brown","Nogofski"};
String[] idNumbers = {"R111111","A222222","AB11111", "KR22121","V311133","L242434","P102432"};
String[] employeeNum = {"1111","2222","3333", "4444","5555","6666","7777"};
List<Employee> list = new ArrayList<>();
for(int i=0;i<firstName.length();i++){
list.add(new(Employee(firstName[i],lastName[i],idNumbers[i],employeeNumber[i]))}
}}