将大型交换机转换为哈希(或其他推荐)

发布于 2024-12-09 13:28:17 字数 652 浏览 3 评论 0原文

我目前有一个 Android 应用程序,可以显示渡船的时间表。该应用程序可以显示完整的时间表(只是一个巨大的列表),但该应用程序的卖点是它将显示接下来两艘渡轮的出发时间以及距离当前出发时间还有多长时间。

我对 Java 比较陌生,目前在我的代码中使用大型 Switch() 语句。基本上,它获取当前的手机时间,并将其与时间表中的所有时间进行比较,然后显示接下来的两个出发时间,然后计算当前时间和出发时间之间的差异。

我确信 switch 语句对于速度目的和代码更改目的不是最好的主意。例如,如果一次更改了一堆代码行,则要进入并修复该一次更改。此外,如果整个时间表发生变化,每个人都必须更新他们的应用程序才能使时间更改生效。我理想的情况是在我的网络服务器上的某个位置存储一个文件,该文件可以下载并插入到哈希映射(我认为是正确的术语)中,如果时间发生变化,该文件将加载新的时间表。

不确定这有多令人困惑,但如果有人可以解释我如何使用哈希图或您可能推荐的其他东西来完成此任务,我将不胜感激。目前,变量是两个渡轮码头以及一周中的哪一天,因为时间表每天都会变化(周一、周二至周五、周六、周日)。

下面是该应用程序的屏幕截图,如果我的帖子不清楚,您可以理解它。先感谢您。

屏幕截图:

screenshot

I currently have an Android application that displays a schedule for a ferry boat. The application can display the full schedule (just a giant list), but the selling point in the application is it will display when the next two ferries are departing and how long from the current time that departure is.

I am relatively new to Java and currently use large Switch() statements in my code. Basically it gets the current phone time and compares it to all of the times in the schedule at which point it displays the next two departure times and then calculates the difference between current time and the departure times.

I am sure that a switch statement is not the best idea for speed purposes as well as code changing purposes. For example if one time changes its a bunch of lines of code to go in and fix for that one time change. Also if the entire schedule changes everyone has to update their app for the time change to take effect. My ideal situation would be to store a file somewhere on my webserver that could be downloaded and inserted into a hashmap (I think is the correct term) that would load the new schedule if there was a time change.

Not sure how confusing this is, but it would be greatly appreciated if someone could explain how I might use a hashmap or something else you might recommend to get this task accomplished. Currently the variables are the two ferry terminals as well as the day of the week since the schedule changes per day (monday, tues-friday, saturday, sunday).

Below is a screenshot of the application so you can understand it if my post wasn't clear. Thank you in advance.

Screenshot:

screenshot

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ι不睡觉的鱼゛ 2024-12-16 13:28:18

将计划对象存储在排序数组中。然后,您可以在数组中二分查找第一个大于当前时间的值。您可能会使用一些由位置和一周中适用的日期组成的父数组。

您可以轻松地将这种数据结构写入读取和读取的文件中。由应用程序解析以进行更新,而不是编译到代码中。

这个的详细情况?首先,了解 Android 中的资源。如果不存在更新的计划,则回退到默认资源。

其次,使用 HTTP 头请求来检查是否存在较新的文件。如果是,则解析、下载并下载保存状态。 使用“保存实例状态”保存 Android Activity 状态

最后,XML 对于数据分发来说很方便,即使速度不快。每个人都理解它并且很容易更新或移交。

<ferry location=0 time=2045>
    <day>1</day>
    <day>2</day>
    <day>3</day>
    <day>4</day>
    <day>5</day>
</ferry>

<ferry location=0 time=0800>
    <day>6</day>
</ferry>

Store the schedule objects in a sorted array. You can then binary search the array for the first value greater than the current time. You'll probably use some parent array consisting of the location and applicable day of the week.

You can easily write that kind of data structure to a file that is read & parsed by the application for updates instead of being compiled into the code.

Details of this? First, understand resources in Android. If no updated schedule exists, fall back to the default resource.

Second, use an HTTP head request to check if a newer file exists. If it does, parse, download & save state. Saving Android Activity state using Save Instance State.

Finally, XML is handy for data distribution, even if it's not fast. Everybody understands it and it's easy to update or hand off.

<ferry location=0 time=2045>
    <day>1</day>
    <day>2</day>
    <day>3</day>
    <day>4</day>
    <day>5</day>
</ferry>

<ferry location=0 time=0800>
    <day>6</day>
</ferry>
等往事风中吹 2024-12-16 13:28:18

您将需要诸如数据库之类的东西来保存时间表数据。这将帮助您将代码与数据分开。我对 Android 不熟悉,但我认为设备上有一个连接 sqlite 数据库的接口。

此外,由于这是小型设备上的应用程序,您可以通过互联网连接连接到服务器上的日程数据库。这样,您只需在一个位置(在服务器上)维护计划数据,并且客户端将始终使用最新的数据。

You will need something like a database to hold the schedule data. That will help you to seperate code from data. I'm not familiar with Android but i think there is a interface to sqlite database on the device.

Further, as this is an application on a small device you may connect to the schedule database on a server thru the internet connection. That way you have to maintain schedule data only in one place (on the server) and clients will use always up to date data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文