如何打印数组的某个部分?

时间:2016-07-09 04:18:01

标签: java

我在代码中遇到一个小错误需要帮助。我有一个程序有4个String数组,我必须使用构造函数组合并打印在不同的位置。我需要编写一个方法:Employee[] searchWithId(Employee[] list, String search),它在数组中搜索一个字符串,并返回匹配的string以及其他相应的信息。

例如:

searchWithId(list, "P102432"); // search the list for the given id

输出:

Searching for the id number:P102432 ...
Found the record for the id number:P102432

first name:Amber    Last Name:Nogofski
Id number:P102432
Employee number:No employee number has been assigned yet!

到目前为止我的代码:

员工类:

public static 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!\n";
        }
        return result + getEmployeeNumber() + "\n";
    }

}

我的主要和其他方法:

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

    Employee[] list = new Employee[firstNames.length];
    list = listOfEmployees(firstNames, lastNames, idNumbers); // create the list of employees in one array    
    System.out.println("List of employees before sorting...\n");
    printEmployeeList(list); //print the list of employees

    System.out.println(); // new line
    searchWithId(list, "P102432"); // search the list for the given id
    searchWithLastName(list, "Bond"); // search the list for the given last name

    System.out.println(); // new line
    searchWithId(list, "P1024444"); // search the list for the given id
    searchWithLastName(list, "BoNd"); // search the list for the given last name

    System.out.println();// new line
    sortWithFirstName(list); // sort the employee list and then call appropriate method to print it.
    list = assignEmployeeNum(list, employeeNum); // assign the employee number to the employees

    System.out.println("+++After adding the employee number to the list+++");// new line
    printEmployeeList(list); // print the list again with the employee number

    searchWithEmployeeNum(list, "5555"); // search the list for the given employee number 
    sortWithFirstName(list); // sort the employee list and then call appropriate method to print it.
}

public static Employee[] listOfEmployees(String[] firstName, String[] lastName, String[] idNumber) {
    Employee[] list = new Employee[firstName.length];
    for (int i = 0; i < list.length; i++) {
        list[i] = new Employee(firstName[i], lastName[i], idNumber[i]);
    }
    return list;
}

public static void printEmployeeList(Employee[] list) {
    Arrays.stream(list).forEach(System.out::println);
}

public static Employee[] searchWithId(Employee[] list, String search) {
    System.out.println("Searching for the id number: " + search);
    for (int i = 0; i < list.length; i++) {
        if (list[i].getIdNumber().equals(search)) {
            System.out.println("Found id number: " + search);
            //Arrays.toString(list); <- my try
        }
    }
    return list;
}

2 个答案:

答案 0 :(得分:0)

将您的searchWithId方法更改为我的:

第一种方法是使用ArrayList的{​​{1}}

Employee

或者您可以使用员工阵列来执行此操作

public static Employee[] searchWithId(Employee[] list, String search) {
    System.out.println("Searching for the id number: " + search);
    ArrayList<Employee> search_result = new ArrayList<>();
    for (Employee employee : list) {
        if (employee.getIdNumber().equals(search)) {
            search_result.add(employee);
        }
    }
    return search_result.toArray(new Employee[search_result.size()]);
}    

即使你可以使用

public static Employee[] searchWithId(Employee[] list, String search) {
    System.out.println("Searching for the id number: " + search);
    Employee search_result[] = new Employee[list.length];
    int index = 0;
    for (Employee employee : list) {
        if (employee.getIdNumber().equals(search)) {
            search_result[index] = employee;
            index++;
        }
    }
    return Arrays.copyOfRange(search_result, 0, index);
}

并返回public static Employee[] searchWithId(Employee[] list, String search) { System.out.println("Searching for the id number: " + search); Employee first_search_result[] = new Employee[list.length]; int index = 0; for (Employee employee : list) { if (employee.getIdNumber().equals(search)) { first_search_result[index] = employee; index++; } } Employee final_search_result[] = new Employee[index]; for (int i=0; i<first_search_result.length;i++) { if (first_search_result[i].getIdNumber().equals(search)) { final_search_result[i] = first_search_result[i]; } } return final_search_result; } ,以避免使用final_search_result

上述方法将以任何方式返回Arrays.copyOfRange id

的所有员工的列表

答案 1 :(得分:0)

您可以按照以下方式执行此操作:

import java.util.Arrays;

public static Employee[] searchWithId(Employee[] list, String search) {
   System.out.println("Searching for the id number: " + search);            
   Employee[] filteredEmployees = new Employee[list.length];
   int index = 0;
   for (int i = 0; i < list.length; i++) {
      if (list[i].getIdNumber().equalsIgnoreCase(search)) {
         filteredEmployees[index++] = list[i];
      }
   }
   // It'll remove the null values:
   return Arrays.copyOfRange(filteredEmployees, 0, index);
}

Java 8版本:

import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;

public static Employee[] searchWithId(Employee[] list, String search) {
   System.out.println("Searching for the id number: " + search);
   return Arrays.asList(list)
            .stream()
            .filter(e -> e.idNumber.equalsIgnoreCase(search))
            .collect(Collectors.toList())
            .toArray(new Employee[list.length]);
}