简体   繁体   English

如何在java中将不同数组的字符串添加到一个数组中

[英]how to add strings from different arrays into one array in java

Hello I'm a bit confused with some coding problem I am trying to solve. 您好我对我试图解决的一些编码问题感到困惑。

I have a few string arrays: 我有几个字符串数组:

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"};

I have to create one array and somehow organize the corresponding pieces of information provided above in the method Employee[] list = new Employee[firstNames.length]; list = listOfEmployees(firstNames,lastNames,idNumbers); // create the list of employees in one array 我必须创建一个数组,并以某种方式组织上面提供的方法Employee[] list = new Employee[firstNames.length]; list = listOfEmployees(firstNames,lastNames,idNumbers); // create the list of employees in one array Employee[] list = new Employee[firstNames.length]; list = listOfEmployees(firstNames,lastNames,idNumbers); // create the list of employees in one array

I started writing out the method: 我开始写出这个方法:

public static Employee[] listOfEmployees(String[] firstName, String[]             
lastName, String[] idNumber){

}

but not sure how to approach this. 但不知道如何处理这个问题。 also not sure if my parameters are correct. 也不确定我的参数是否正确。

the end result is supposed to look like this: 最终结果应该是这样的:

Employee #1 
    first name:Fred Last Name:Bond
    Id number:R111111

. . .

Employee #2 
    first name:John Last Name:Kates
    Id number:A222222

and so on.. 等等..

thanks in advance. 提前致谢。

EDIT : 编辑

Employee class: 员工类:

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();
    }

}

Please try the following: 请尝试以下方法:

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;
 }

To print the array returned by the above function, you may use: 要打印上述函数返回的数组,您可以使用:

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("------------------------------------");
    }
}

And call them by following statement: 并通过以下声明打电话给他们:

printEmployees(listOfEmployees(firstNames,lastNames,idNumbers));

Do a for loop and use the Employee constructor to initialize the objects: 执行for循环并使用Employee构造函数初始化对象:

Employee[] list = new Employee[firstNames.length];
for (int i = 0; i < firstName.length; i++) {
    list[i] = new Employee(firstName[i], lastName[i] ...
}

Try this 尝试这个

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]))}

}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM