如何将临时数组中每5个员工详情元素存入string类型数组?
Got it, here's a straightforward way to group your temparray elements into chunks of 5 for each employee. The core idea is to loop through the array in increments of 5, extracting each block of 5 elements and storing them as a separate string array in a larger collection.
Let's break this down with code examples in a few common languages:
C# Example
Since you mentioned string type arrays, C# is a natural fit here:
// Assume temparray is your existing string array with all employee lines List<string[]> employeeList = new List<string[]>(); // Loop through the temp array in steps of 5 for (int i = 0; i < temparray.Length; i += 5) { // Guard against index out of bounds if the array length isn't a multiple of 5 if (i + 4 < temparray.Length) { // Create a new 5-element array for the current employee string[] employeeDetails = new string[5]; employeeDetails[0] = temparray[i]; employeeDetails[1] = temparray[i+1]; employeeDetails[2] = temparray[i+2]; employeeDetails[3] = temparray[i+3]; employeeDetails[4] = temparray[i+4]; employeeList.Add(employeeDetails); } else { // Optional: Handle incomplete employee data Console.WriteLine($"Warning: Incomplete employee entry starting at index {i}"); } } // Convert to a string array of arrays if needed string[][] employeeArray = employeeList.ToArray();
Python Example
If you're using Python, list slicing makes this super concise:
# Assume temparray is your list of lines from the file employee_list = [] # Iterate in steps of 5 for i in range(0, len(temparray), 5): # Slice the list to get the 5 elements for one employee employee_details = temparray[i:i+5] # Only add if we have a full set of 5 elements if len(employee_details) == 5: employee_list.append(employee_details) else: print(f"Warning: Incomplete employee data starting at index {i}") # employee_list now contains each employee's details as a sub-list
Java Example
For Java, use an ArrayList to dynamically build your collection:
// Assume temparray is your String array with all lines ArrayList<String[]> employeeList = new ArrayList<>(); for (int i = 0; i < temparray.length; i += 5) { if (i + 4 < temparray.length) { String[] employeeDetails = new String[5]; // Copy 5 elements from temparray starting at index i System.arraycopy(temparray, i, employeeDetails, 0, 5); employeeList.add(employeeDetails); } else { System.out.println("Warning: Incomplete employee entry starting at index " + i); } } // Convert to a 2D String array if needed String[][] employeeArray = employeeList.toArray(new String[employeeList.size()][]);
Quick Notes:
- Index Safety: Always include a check to avoid out-of-bounds errors if your input array doesn't have a multiple of 5 elements.
- Flexibility: Using a list (instead of a fixed-size array) lets you add employee groups dynamically without knowing the final count upfront.
- Readability: Incrementing the loop by 5 makes it clear you're processing chunks of employee data, which helps with future maintenance.
Once done, each entry in your final array/list will hold all 5 details for a single employee, in the same order they appeared in the original file.
内容的提问来源于stack exchange,提问作者Roid

