0

我正在做一些火车申请。在应用程序中,我正在维护火车时间。从我的数据库中,我可以像这样以字符串数组列表的形式获取时间

   train_schedule_time------>[8.2, 13.55, 0.45]

现在我想用这样的相应分钟按小时分隔我的数组......

在此处输入图像描述

我正在使用 StringTokenizer 并拆分为小时数组和分钟数组。但我无法将我的小时与多分钟分组,我必须在列表视图中显示。我怎样才能做到这一点?有谁能够帮我?预先感谢

4

2 回答 2

2

您可以拆分字符串。例如:

String train_schedule_tim = "8.2, 13.55, 0.45";

String[] hours = train_schedule_tim.split(", ");

String hour1 = hours[0].split(".")[0];
String mins1 = hours[0].split(".")[1];

String hour2 = hours[1].split(".")[0];
String mins2 = hours[1].split(".")[1];

如果您想在几分钟内保持冷静,您可以执行以下操作(您更喜欢使用 Integer 或 String):

Map<Integer, List<Integer>> hours = new HashMap<Integer, List<Integer>>();

List<Integer> minutes = new ArrayList<Integer>();
minutes.add(15);
minutes.add(30);
minutes.add(45);

hours.put(8, minutes);

然后,你可以这样做:

for (Integer h : hours.keySet()) {
    List<Integer> mins = hours.get(h);
}
于 2013-10-10T06:07:58.890 回答
0

例如,您可以使用StringTokenizer类(来自java.util):

StringTokenizer tokens = new StringTokenizer(CurrentString, ":");
String first = tokens.nextToken();// this will contain "hours"
String second = tokens.nextToken();// this will contain "Minutes"
// in the case above I assumed the string has always that syntax (12:30)
// but you may want to check if there are tokens or not using the hasMoreTokens method
于 2013-10-10T06:18:47.107 回答