使用Java中的循环创建变量名? [英] Create variable names using a loop in Java?
问题描述
请看下面的代码,用于为每个月的开始和结束生成时间戳在一个财政年度。
int year = 2010;
//财政年度从9月到8月这样早期是那些年= FY-1和晚期的年份是那些年= FY
int [] earlyMonths = {8,9,10,11}; // Sept to Dec
int earlyYear = year -1; (int i:earlyMonths)
{
month = i;
日历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}; // 1月至8月
(int i:lateMonths){
month = i;
日历cal = Calendar.getInstance();
cal.clear();
cal.set(年,月,1,0,0,0);
Long start = cal.getTimeInMillis();
cal.clear();
cal.set(年,月,1);
lastDayofMonth = cal.getActualMaximum(GregorianCalendar.DAY_OF_MONTH);
cal.set(年,月,lastDayofMonth,23,59,59);
Long end = cal.getTimeInMillis();
}
到目前为止这么好,但为了使用这些结果,我需要这些时间戳被输出到由月份命名的变量(在代码中稍后的准备语句中使用),例如SeptStart = sometimestamp,SeptEnd =某些时间戳等。
不知道是否可以根据每个循环的结果声明新的变量。任何想法?
为什么不使用一个 地图 ?
所有你想要一个容器的一些值,并用指定的名称来解决它。
所以只需要将变量名称设为您的密钥和变量值即可,ehm,value。
/ p>
首先,去一个 Treemap 而不是地图。
另外,为了保留字典列表顺序,将您的月数填充为零左右,并使用开始和结束作为分隔符
所以你将拥有:
code> 01_begin
01_end
02_begin
...
10_begin
10_end
...
当您访问树状图时,将以正确的顺序打印。
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?
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:
01_begin
01_end
02_begin
...
10_begin
10_end
...
which will get printed in the correct order when you visit the treemap.
这篇关于使用Java中的循环创建变量名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!