如何将存储灯光开关时序数据的两个ArrayList存入动态三维数组
Convert Lighting Switch Data from ArrayLists to a Dynamic 3D Array
Hey there! Let's break down how to turn those two ArrayLists (one for datetime strings, one for light status values) into a practical dynamic 3D array. First, let's define a logical structure for the 3D array that makes sense with your time-series data:
- 1st Dimension: Groups of records by date (e.g., all entries from 2015-11-09, then 2015-11-10, etc.)
- 2nd Dimension: Individual switch events within each date group
- 3rd Dimension: The two pieces of data for each event: the full datetime string and the status (1 = on, 0 = off)
Step-by-Step Implementation (Java Example)
Since you're using ArrayLists, I'll assume you're working in Java. Here's a complete, commented solution:
import java.util.*; public class LightingDataConverter { public static void main(String[] args) { // Your existing ArrayLists (populated with your sample data) ArrayList<String> datetimeList = new ArrayList<>(Arrays.asList( "2015-11-09T10:04:22", "2015-11-09T11:45:14", "2015-11-09T11:45:32", "2015-11-09T15:13:56", "2015-11-10T10:17:17", "2015-11-10T11:20:04", "2015-11-10T12:28:41", "2015-11-10T13:04:46", "2015-11-10T13:05:48", "2015-11-10T13:35:15", "2015-11-11T13:10:04", "2015-11-11T15:46:18" )); ArrayList<Integer> statusList = new ArrayList<>(Arrays.asList( 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 )); // First: Validate both lists have the same length (critical for data integrity) if (datetimeList.size() != statusList.size()) { throw new IllegalArgumentException("Datetime and status lists must match in length!"); } // Group records by date using a LinkedHashMap to preserve chronological order Map<String, List<Object[]>> dateGroupedRecords = new LinkedHashMap<>(); for (int i = 0; i < datetimeList.size(); i++) { String fullDatetime = datetimeList.get(i); Integer status = statusList.get(i); // Extract the date part (split on "T" to separate date and time) String dateKey = fullDatetime.split("T")[0]; // Create a new list for the date if it doesn't exist yet dateGroupedRecords.computeIfAbsent(dateKey, k -> new ArrayList<>()); // Add the current event as a 2-element array (datetime + status) dateGroupedRecords.get(dateKey).add(new Object[]{fullDatetime, status}); } // Convert the grouped data into a 3D array Object[][][] threeDimensionalArray = new Object[dateGroupedRecords.size()][][]; int dateGroupIndex = 0; for (List<Object[]> dailyRecords : dateGroupedRecords.values()) { // Initialize the 2nd dimension for the current date's records threeDimensionalArray[dateGroupIndex] = new Object[dailyRecords.size()][2]; // Populate each event in the 3rd dimension for (int eventIndex = 0; eventIndex < dailyRecords.size(); eventIndex++) { threeDimensionalArray[dateGroupIndex][eventIndex] = dailyRecords.get(eventIndex); } dateGroupIndex++; } // Optional: Print the array to verify the result for (Object[][] dailyGroup : threeDimensionalArray) { System.out.println("\n--- Date Group ---"); for (Object[] event : dailyGroup) { System.out.printf("Datetime: %s | Status: %d%n", event[0], event[1]); } } } }
Key Notes:
- Dynamic Structure: The array adjusts automatically to the number of dates and events per date, so it works even if your dataset grows.
- Order Preservation: We use
LinkedHashMapto keep the date groups in the same order as your original data. - Data Integrity: The initial check ensures you don't have mismatched datetime/status pairs.
If You Don't Need Date Grouping
If you just want a flat 3D array (no date grouping), you can simplify the code to this:
Object[][][] flatThreeDArray = new Object[datetimeList.size()][1][2]; for (int i = 0; i < datetimeList.size(); i++) { flatThreeDArray[i][0][0] = datetimeList.get(i); flatThreeDArray[i][0][1] = statusList.get(i); }
This structure uses the first dimension for each event, the second dimension as a single placeholder, and the third dimension for the datetime/status pair.
内容的提问来源于stack exchange,提问作者thelaw
相关产品推荐
相关产品推荐

