public void two(final BeanForm[] captureddata)
{
for (BeanForm form : captureddata)
{
if (form.getCyclicType() != null)
{
logger.info("The Cyclic Type is"+ form.getCyclicType().value());
}
if (form.getTicketType() != null)
{
logger.info("The Ticket Type is"+ form.getTicketType().value());
}
}
}上面的代码运行良好,但是我在日志文件中看到的输出是(如果BeanForm的长度是2)
11/Nov/2011 20:15:51 - The Cyclic Type is DTI
11/Nov/2011 20:15:51 - The Ticket Type is MMTS
11/Nov/2011 20:15:51 - The Cyclic Type is DTI
11/Nov/2011 20:15:51 - The Ticket Type is MMTS我只想知道是否有可能获得数组的详细信息,例如,此数据属于哪个数组
The array[1] Cyclic Type is DTI
The array[2] Cyclic Type is SAG发布于 2011-11-11 23:16:48
只需使用外部计数:
public void two(final BeanForm[] captureddata)
{
int count = 0;
for (BeanForm form : captureddata)
{
if (form.getCyclicType() != null)
{
logger.info(count + " The Cyclic Type is"+ form.getCyclicType().value());
}
if (form.getTicketType() != null)
{
logger.info(count + " The Ticket Type is"+ form.getTicketType().value());
}
count++;
}
}或者作为一个普通的for循环
public void two(final BeanForm[] captureddata)
{
for (int i=0; i<captureddata.length; i++)
{
BeanForm form = capturedata[i];
if (form.getCyclicType() != null)
{
logger.info(i+ " The Cyclic Type is"+ form.getCyclicType().value());
}
if (form.getTicketType() != null)
{
logger.info(i+ " The Ticket Type is"+ form.getTicketType().value());
}
}
}发布于 2011-11-11 23:15:57
如果你指的是循环中的索引-不是。您需要显式地执行此操作:
for (int i = 0; i < capturedData.length; i++)
{
BeanForm form = capturedData[i];
// Now you have both form and i.
}发布于 2011-11-11 23:16:36
一种可能的方法是在for循环中维护一个计数器,您可以将该计数器与array[]一起使用,如下所示:
int i=0;
for-each loop
{
//print array[i]
//increment i
}https://stackoverflow.com/questions/8095953
复制相似问题