在 Java 中使用循环创建变量名?
第一次发帖,长期读者,所以请对我温柔点:)
请参阅以下代码,该代码可以为我生成一个财政年度中每个月的开始和结束的时间戳。
int year = 2010;
// Financial year runs from Sept-Aug so earlyMonths are those where year = FY-1 and lateMonths are those where year = FY
int[] earlyMonths = {8, 9, 10, 11}; // Sept to Dec
int earlyYear = year -1;
for (int i : earlyMonths) {
month = i;
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(earlyYear,month,1,0,0,0);
Long start = cal.getTimeInMillis();
cal.clear();
cal.set(earlyYear,month,1);
lastDayofMonth = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
cal.set(earlyYear,month,lastDayofMonth,23,59,59);
Long end = cal.getTimeInMillis();
}
int[] lateMonths = {0, 1, 2, 3, 4, 5, 6, 7}; // Jan to Aug
for (int i : lateMonths) {
month = i;
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(year,month,1,0,0,0);
Long start = cal.getTimeInMillis();
cal.clear();
cal.set(year,month,1);
lastDayofMonth = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
cal.set(year,month,lastDayofMonth,23,59,59);
Long end = cal.getTimeInMillis();
}
到目前为止一切顺利,但为了使用这些结果,我需要将这些时间戳输出到按月份命名的变量(将在代码中稍后的准备好的语句中使用。例如 SeptStart = 有时戳,SeptEnd = 一些时间戳等。
我不知道是否可以根据每个循环的结果声明新变量。
first time poster, long time reader so be gentle with me :)
See the following code which works to generate me timestamps for the beginning and end of every month in a financial year.
int year = 2010;
// Financial year runs from Sept-Aug so earlyMonths are those where year = FY-1 and lateMonths are those where year = FY
int[] earlyMonths = {8, 9, 10, 11}; // Sept to Dec
int earlyYear = year -1;
for (int i : earlyMonths) {
month = i;
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(earlyYear,month,1,0,0,0);
Long start = cal.getTimeInMillis();
cal.clear();
cal.set(earlyYear,month,1);
lastDayofMonth = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
cal.set(earlyYear,month,lastDayofMonth,23,59,59);
Long end = cal.getTimeInMillis();
}
int[] lateMonths = {0, 1, 2, 3, 4, 5, 6, 7}; // Jan to Aug
for (int i : lateMonths) {
month = i;
Calendar cal = Calendar.getInstance();
cal.clear();
cal.set(year,month,1,0,0,0);
Long start = cal.getTimeInMillis();
cal.clear();
cal.set(year,month,1);
lastDayofMonth = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
cal.set(year,month,lastDayofMonth,23,59,59);
Long end = cal.getTimeInMillis();
}
So far so good, but in order to use these results I need these timestamps to be output to variables named by month (to be used in a prepared statement later in the code. e.g. SeptStart = sometimestamp, SeptEnd = some timestamp etc etc.
I don't know if it is possible to declare new variables based on the results of each loop. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
为什么不使用 Map?
毕竟,您希望有一个用于某些值的“容器”,并使用指定的名称对其进行寻址。
因此,只需将“变量名称”设置为键,将“变量值”设置为值即可。
编辑是因为您想要一个排序集合:
首先,选择 树形图而不是地图。
另外,为了保留词典顺序,请将月份数字向左填充零标准化,并使用“开始”和“结束”作为分隔符,
这样您将得到:
当您访问树形图时,它将以正确的顺序打印。
Why not use a Map?
After all you want to have a "container" for some value and address it with a specified name.
So just make the "variable name" your key and "variable value" your, ehm, value.
Edited because you wanted a Sorted collection:
First of all, go for a Treemap instead of a Map.
Also, to preserve lexicograph order, normalize your month number padding zeroes to the left, and use "begin" and "end" as delimiters
So you will have:
which will get printed in the correct order when you visit the treemap.
使用
Map
代替生成变量名称,其中键是月份名称,值是该月份的值。您可以执行
startDates.put("September", some date)
而不是Date septStart = some date
或者更好,将值存储在数组中,其中索引为月份数:
startDates[8] = some date // 0-indexed!
Instead of generating variable names, use a
Map
, where the key is a month name, and the value is the value for that month.Instead of
Date septStart = some date
you can dostartDates.put("September", some date)
Or better yet, store the values in an array, where the index is the month number:
startDates[8] = some date // 0-indexed!
您不能动态声明变量。我建议声明两个时间戳数组(一个用于开始,一个用于结束)并填充它们。然后使用这些数组而不是命名变量,或者手动声明变量并手动设置它们。
You can't declare variables dynamically. I'd suggest to declare two arrays of timestamps (one for the beginnings and one for the endings) and fill them. Then either use these arrays instead of named variables or declare the variables by hand and set them manually.
将值存储在数组中将允许您通过 Calendar.Month 常量访问它们
Storing the values in an array would allow you to access them by the Calendar.Month constants
地图确实似乎是一个很好的解决方案:)我是java / jsp(以及大多数一般编程!)的新手,所以我不知道这有多简单:)
以下代码:
给我以下输出:
我很好奇为什么要这样排序?就功能而言并不重要,但出于调试目的是否可以对哈希图进行排序?
感谢大家提出的宝贵建议:)
A map does indeed seem to be a good solution :) I am new to java / jsp (and most programming in general!) so I was not aware of how simple this is :)
The following code:
Gives me the following output:
I was curious why it was ordered in such a way? It's not important as far as the functionality is concerned, but for debugging purposes is it possible to order the hashmap?
Thanks for the great suggestions all round folks :)
为了完成,我发布了完整的代码。它从数据库中提取有关我们生产服务器的可用性信息以生成管理报告。这是我在 JSP 中做过的最复杂的事情:)
我确信很多事情可以做得更优雅,所以请随时提出建议:)(显然数据库密码等已被 XXX 删除)
For completion, I post the full code. It pulls availability information about our production servers from a database for a management report. It's the most complicated thing I have done in JSP yet :)
I am sure lots of things could be done more elegantly, so please feel free to advise :) (obviously db passwords etc are XXX'ed out)