简体   繁体   中英

Storing the values of two array lists on a dynamic 3 dimensional array

I have the following data which shows the exact time and date when someone turns on and off their light in their room stored into 2 array lists:

  1. 2015-11-09T10:04:22 1
  2. 2015-11-09T11:45:14 0
  3. 2015-11-09T11:45:32 1
  4. 2015-11-09T15:13:56 0
  5. 2015-11-10T10:17:17 1
  6. 2015-11-10T11:20:04 0
  7. 2015-11-10T12:28:41 1
  8. 2015-11-10T13:04:46 0
  9. 2015-11-10T13:05:48 1
  10. 2015-11-10T13:35:15 0
  11. 2015-11-11T13:10:04 1
  12. 2015-11-11T15:46:18 0

First array list contains the datetime and second array list contains the value that shows if someone turned on/off the light.

I want to sort the data into a dynamic three dimensional array where the date (without the time) will be the key and for each date I can store the time I observed a change and what kind of change it was (0 or 1 in this case).

What type of data structure should I use? I would appreciate some guidance.

An SQL table could also be your option.

Your requirement for filtering on dates, sorting on times and detecting on/off gets satisfied.

The data structures that comes into mind are LinkedList and ArrayList . Here you have two options.

  1. If you know the number of dates you'll be sorting then an array of LinkedList would suffice.

  2. If you don't know the number of dates you'll be sorting then a use an ArrayList of LinkedList

I would consider a Map as a good container for your use case. The map allows the date to be the key, and creating a SwitchEvent class (holds the time and switch state on/off) allows a List to be used to store the events for each day.

Map<LocalDate, List<SwitchEvent>> eventContainer

Example usage follows:

public class LightSwitchMain {

public static void main(String[] args) {

    Map<LocalDate, List<SwitchEvent>> eventContainer = new HashMap<>();

    // Example of a light switch OFF on Jan 1, 2016 at 9:45)
    LocalDate eventDate1 = LocalDate.of(2016, 1, 1);
    SwitchEvent event1 = new SwitchEvent(LocalTime.of(9, 45), 0);

    addSwitchEvent(eventContainer, eventDate1, event1);

    // Light goes on
    SwitchEvent event2 = new SwitchEvent(LocalTime.of(9, 55), 1);
    addSwitchEvent(eventContainer, eventDate1, event2);

}

static void addSwitchEvent(Map<LocalDate, List<SwitchEvent>> eventContainer, LocalDate eventDate, SwitchEvent event) {
    List<SwitchEvent> dayEvents = eventContainer.get(eventDate);
    if (dayEvents == null) {
        dayEvents = new ArrayList<SwitchEvent>();
        eventContainer.put(eventDate, dayEvents);
    }
    dayEvents.add(event);
}

static class SwitchEvent {
    final LocalTime eventTime;  // Time of the light switch
    final int switchState;      // 0=off, 1=on
    public SwitchEvent(LocalTime eventTime, int switchState) {
        this.eventTime = eventTime;
        this.switchState = switchState;
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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