需要建议合适的数据结构

时间:2015-08-04 05:55:25

标签: java arraylist data-structures stack processing

Day1
  Hour1
    Minute1 
​      Location1(i.e. lat,lon)
      Location2 
    Minute2 
  Hour2
  Hour3​ 
Day2 
  Hour1 
    Minute1
​    Minute2​
  Hour2​

根据时间戳(日期:小时:分钟:秒),有多个用户拥有用户位置。 无论日期如何(即只有小时和分钟),我都需要将所有用户位置数据与时间堆叠起来

例如:将每日位置数据堆叠成24小时(24 * 60分钟)时间线。

我仅使用24个阵列列表执行小时分辨率并检查每个时间戳的小时值,然后使用switch语句将每个对应的位置值分配给其中一个阵列列表。

进入分钟分辨率时,拥有1440(24 * 60)阵列列表是不切实际的。如果你能提出任何有效的方法,我将不胜感激。我希望在解释我的问题时我能说清楚。

提前致谢, Hasala

编辑:以下是写入小时解析的代码。

//24 ArrayLists to store users with location according to hours of the day
ArrayList<Location> hour1 = new ArrayList<Location>();
ArrayList<Location> hour2 = new ArrayList<Location>();
ArrayList<Location> hour3 = new ArrayList<Location>();
ArrayList<Location> hour4 = new ArrayList<Location>();
ArrayList<Location> hour5 = new ArrayList<Location>();
ArrayList<Location> hour6 = new ArrayList<Location>();
ArrayList<Location> hour7 = new ArrayList<Location>();
ArrayList<Location> hour8 = new ArrayList<Location>();
ArrayList<Location> hour9 = new ArrayList<Location>();
ArrayList<Location> hour10 = new ArrayList<Location>();
ArrayList<Location> hour11 = new ArrayList<Location>();
ArrayList<Location> hour12 = new ArrayList<Location>();
ArrayList<Location> hour13 = new ArrayList<Location>();
ArrayList<Location> hour14 = new ArrayList<Location>();
ArrayList<Location> hour15 = new ArrayList<Location>();
ArrayList<Location> hour16 = new ArrayList<Location>();
ArrayList<Location> hour17 = new ArrayList<Location>();
ArrayList<Location> hour18 = new ArrayList<Location>();
ArrayList<Location> hour19 = new ArrayList<Location>();
ArrayList<Location> hour20 = new ArrayList<Location>();
ArrayList<Location> hour21 = new ArrayList<Location>();
ArrayList<Location> hour22 = new ArrayList<Location>();
ArrayList<Location> hour23 = new ArrayList<Location>();
ArrayList<Location> hour24 = new ArrayList<Location>();
//

//
void processToHours(){  
  for (int p=0;p<timeStamps.size();p++){
    String time = timeStamps.get(p);
    Location userLoc = new Location(Double.parseDouble(lat.get(p)),Double.parseDouble(lon.get(p)));
    int hour = convertTime(time);
        //
        switch (hour) {
            case 0:  hour1.add(userLoc);
                     break;
            case 1:  hour2.add(userLoc);
                     break;
            case 2:  hour3.add(userLoc);
                     break;
            case 3:  hour4.add(userLoc);
                     break;
            case 4:  hour5.add(userLoc);
                     break;
            case 5:  hour6.add(userLoc);
                     break;
            case 6:  hour7.add(userLoc);
                     break;
            case 7:  hour8.add(userLoc);
                     break;
            case 8:  hour9.add(userLoc);
                     break;
            case 9:  hour10.add(userLoc);
                     break;
            case 10: hour11.add(userLoc);
                     break;
            case 11: hour12.add(userLoc);
                     break;
            case 12: hour13.add(userLoc);
                     break;
            case 13: hour14.add(userLoc);
                     break;         
            case 14: hour15.add(userLoc);
                     break;
            case 15: hour16.add(userLoc);
                     break;
            case 16: hour17.add(userLoc);
                     break;
            case 17: hour18.add(userLoc);
                     break;
            case 18: hour19.add(userLoc);
                     break;
            case 19: hour20.add(userLoc);
                     break;
            case 20: hour21.add(userLoc);
                     break;
            case 21: hour22.add(userLoc);
                     break;
            case 22: hour23.add(userLoc);
                     break;
            case 23: hour24.add(userLoc);
                     break;
            default: println("Invalid Time");
                     break;
        }
        //
  }
}
//

3 个答案:

答案 0 :(得分:1)

我会使用Json string来保存数据,然后当我需要数据时,我会解析字符串并相应地提取数据。

例如,我想要第1天,第1小时,第1分钟的数据...然后...... 写函数:

List<String> GetLocation(String jsonStr, String dayNumber, String hrNumber, String miNumber){
List<String> locationList = new ArrayList<String>();
//extract data by parsing json
//add extracted location in list
return locationList;
}

注意:此方法的缺点是每次需要特定数据时都必须解析JSON String

OR 您可以根据数据使用的性质以最佳方式使用此功能。

答案 1 :(得分:1)

当你开始写出list1,list2等等时。你应该考虑一个包含所有其他列表的列表。例如。

class Day{
    List<Hour> hours;
}
class Hour{
    List<Minute> minutes;
}
class Minute{
    List<Location> locations;
}

我也喜欢使用Map而不是列表的想法,这样你就不必用所有可能的小时/分钟来填充列表,例如。

class Day{
    Map<Integer, Hour> hours = new HashMap<>();

    public void addLocation(int hour, int minute, Location location){
        if(!hours.containsKey(hour)){
            hours.put(hour, new Hour());
        }
        hours.get(hour).addLocation(minute, location);
    }
    public Hour getHour(int hour){
        return hours.get(hour);
    }
}

class Hour{
    Map<Integer, Minute> minutes = new HashMap<>();
    public void addLocation(int minute, Location location){
        if(!minutes.containsKey(minute)){
            minutes.put(minute, new Minute());
        }
        minutes.get(minute).addLocation(location);
    }
}

class Minute{
    List<Location> locations = new ArrayList<>();
    public void addLocation(Location location){
        locations.add(location);
    }
}

public class LocationManager{
    Map<Integer, Day> days = new HashMap<>();
    public void addLocation(int day, int hour, int minute, Location location){
        if(!days.containsKey(day)){
            days.put(day, new Day());
        }
        days.get(day).addLocation(hour, minute, location);
    }
}

答案 2 :(得分:0)

听起来我需要你自己的,修改过的Trie(https://en.m.wikipedia.org/wiki/Trie)的实现,其中的关键是数字,其中每个级别对应于小时,分钟或秒,而值是位置。

我现在正在使用手机,但请查看普林斯顿的算法(不确定哪一个,1个或2个)及其实现的键值尝试。

这可能是你的支持结构,但为了良好的编程原则,你仍然需要封装它,因此它具有上述API,出于所有原因:)。