Java 需要一个合适的数据结构的建议吗
有多个用户具有按时间戳显示的用户位置(日期:小时:分钟:秒)。 我需要将所有用户位置数据按时间堆叠,而不考虑日期(即,仅小时和分钟) 例如:将每天的位置数据叠加到24小时(24*60分钟)的时间线中 我这样做只是为了小时分辨率使用24个数组列表,检查每个时间戳的小时值,然后使用switch语句将每个对应的位置值分配给其中一个数组列表 进入分钟分辨率时使用1440(24*60)数组列表是不实际的。如果你能提出任何有效的方法,我将不胜感激。我希望我在解释我的问题时说清楚 提前感谢,, 哈萨拉 编辑:以下是写入小时分辨率的代码Java 需要一个合适的数据结构的建议吗,java,arraylist,data-structures,stack,processing,Java,Arraylist,Data Structures,Stack,Processing,有多个用户具有按时间戳显示的用户位置(日期:小时:分钟:秒)。 我需要将所有用户位置数据按时间堆叠,而不考虑日期(即,仅小时和分钟) 例如:将每天的位置数据叠加到24小时(24*60分钟)的时间线中 我这样做只是为了小时分辨率使用24个数组列表,检查每个时间戳的小时值,然后使用switch语句将每个对应的位置值分配给其中一个数组列表 进入分钟分辨率时使用1440(24*60)数组列表是不实际的。如果你能提出任何有效的方法,我将不胜感激。我希望我在解释我的问题时说清楚 提前感谢,, 哈萨拉 编辑
Day1
Hour1
Minute1
Location1(i.e. lat,lon)
Location2
Minute2
Hour2
Hour3
Day2
Hour1
Minute1
Minute2
Hour2
//24个ArrayList,用于根据一天中的小时数存储具有位置的用户
ArrayList hour1=新建ArrayList();
ArrayList hour2=新建ArrayList();
ArrayList hour3=新建ArrayList();
ArrayList hour4=新建ArrayList();
ArrayList hour5=新建ArrayList();
ArrayList hour6=新建ArrayList();
ArrayList hour7=新建ArrayList();
ArrayList hour8=新建ArrayList();
ArrayList hour9=新建ArrayList();
ArrayList hour10=新建ArrayList();
ArrayList hour11=新建ArrayList();
ArrayList hour12=新建ArrayList();
ArrayList hour13=新建ArrayList();
ArrayList hour14=新建ArrayList();
ArrayList hour15=新建ArrayList();
ArrayList hour16=新建ArrayList();
ArrayList hour17=新建ArrayList();
ArrayList hour18=新建ArrayList();
ArrayList hour19=新建ArrayList();
ArrayList hour20=新建ArrayList();
ArrayList hour21=新建ArrayList();
ArrayList hour22=新建ArrayList();
ArrayList hour23=新建ArrayList();
ArrayList hour24=新建ArrayList();
//
//
void processToHours(){
对于(int p=0;p我将使用它来保存数据,然后当我需要数据时,我将解析字符串并相应地提取数据
例如,我想要第1天、第1小时、第1分钟的数据…然后。。。
编写函数:
//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;
}
//
}
}
//
List GetLocation(字符串jsonStr、字符串dayNumber、字符串hrNumber、字符串miNumber){
List locationList=新的ArrayList();
//通过解析json提取数据
//在列表中添加提取的位置
返回位置列表;
}
注意:此方法的缺点是每次需要特定数据时都必须解析JSON字符串
或您可以根据数据使用的性质以最佳方式使用它。我觉得您需要自己的、经过修改的Trie()实现,其中Trie中的键是数字,每个级别对应于小时、分钟或秒,而值是位置
我现在正在用手机,但看看普林斯顿的算法(不确定是1还是2)以及它们的键值实现
这可以是你的后盾结构,但是为了良好的编程原理,你仍然需要封装它,所以它有一个上面的API来解释所有的原因:)
当你开始写List1、List2等时,你应该考虑一个列表包含你所有的其他列表。例如,
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;
}
上课日{
列出时间;
}
课时{
列出会议记录;
}
课堂记录{
列出地点;
}
我也喜欢使用地图而不是列表的想法,这样你就不必在列表中填入所有可能的小时/分钟,例如
class Day{
List<Hour> hours;
}
class Hour{
List<Minute> minutes;
}
class Minute{
List<Location> locations;
}
上课日{
Map hours=新的HashMap();
公共无效添加位置(整数小时、整数分钟、位置){
如果(!hours.containsKey(小时)){
小时。放置(小时,新小时());
}
小时。获取(小时)。添加位置(分钟,位置);
}
公共小时数(整小时){
返回小时数。获取(小时);
}
}
课时{
Map minutes=newhashmap();
公共无效添加位置(整数分钟,位置){
如果(!minutes.containsKey(分钟)){
分钟。放入(分钟,新分钟());
}
分钟。获取(分钟)。添加位置(位置);
}
}
课堂记录{
列表位置=新的ArrayList();
公共无效添加位置(位置){
位置。添加(位置);
}
}
公共类位置管理器{
映射天数=新建HashMap();
公共无效添加位置(整数天、整数小时、整数分钟、位置){
如果(!天.集装箱(天)){
天。放(天,新的一天());
}
天。获取(天)。添加位置(小时、分钟、位置);
}
}
Day
class(1个字段:Map
key=1..24),Hourclass(1个字段:Map
key=1..59),Minuteclass(1个字段:List
)怎么样?@RC.你能用代码详细说明一下吗?我的意思是请给出一个地图使用的示例。你说的使用是什么意思?@RC.我的意思是如何正确使用地图。我能在一张地图中同时包含小时和分钟吗?如果你真的需要一个小时/分钟的表格,谷歌番石榴有一些实现(参见com.google.common.collect.Table
)谢谢。为什么我们必须在日、时、分课程中使用相同的位置值?你能解释一下吗?你真的不知道,这是我设置结构的方式。一天不知道分钟或位置,只知道小时。所以它会将不知道的信息传递到适当的时间。另一种方法是使用getter。例如day.getHour(hour)。getMinute(minute)。addLocation(location);好的,我将尝试此方法并让您知道输出。我仍在计算您给出的代码。谢谢matt。嗨,我尝试了您的代码。我正在处理IDE时使用此代码。它会给我一个错误,说“函数hasKey(int)不存在”你能帮我解决这个问题吗?谢谢。很抱歉,我会更新这篇文章。使用“containsKey”。谢谢Lopina。我从来没有使用过Trie结构。我会研究它。如果你能给出一些例子,请
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);
}
}