问题描述
在我的数组循环到达最后一个索引,我得到一个异常说,该指数超出范围。我想要的是让它回到第一个索引,直到以Z
等于点击率
。我该怎么做?
我的code:
字符资源;
INT点击率= 10
的char [] =烈焰{'F','L','A','M','E','S'};对于(INT Z = 0; z,其中,CTR-1; Z ++){
RES =(火焰[Z]);
jLabel1.setText(将String.valueOf(RES));
}
您需要使用一个索引仅限于数组的大小。更多precisely和esoterically来说,你需要在for循环迭代{} 0..9映射为火焰阵{0 .. flames.length()的有效指标 - 1
},这是相同的,在这种情况下,向{0..5}
在从0到5的循环迭代,映射是微不足道的。当循环迭代一个第6次,那么你需要回到它映射到数组索引0,当迭代到第7的时候,你把它映射到数组索引1,依此类推。
==原始的方法==
的for(int Z = 0,J = 0; z,其中,CTR-1; Z ++,J ++)
{
如果(J> = flames.length())
{
J = 0; //重新回到起点
}
RES =(火焰[J]);
jLabel1.setText(将String.valueOf(RES));
}
==一个更合适的方式==
然后,你可以通过实现完善这一 flames.length()
是不变的,你搬出一个for循环的。
最终诠释N = flames.length();
对于(INT Z = 0,J = 0; z,其中,CTR-1; Z ++,J ++)
{
如果(J> = N)
{
J = 0; //重新回到起点
}
RES =(火焰[J]);
jLabel1.setText(将String.valueOf(RES));
}
==如何做它==
现在,如果你注意,你可以看到我们只做模运算上的索引。所以,如果我们采用模块化(%)经营者,我们可以简化您的code:
最终诠释N = flames.length();
对于(INT Z = 0; z,其中,CTR-1; Z ++)
{
RES =(火焰[Z%N]);
jLabel1.setText(将String.valueOf(RES));
}
在像这样的问题的工作,想想函数映射,从一个域(在这种情况下,循环迭代),以一个范围(有效的数组索引)。
更重要的是,解决它在纸上之前,你甚至开始code。这将带你很长的路要走朝着解决这些类型的元素的问题。
After my array in the for loop reaches the last index, I get an exception saying that the index is out of bounds. What I wanted is for it to go back to the first index until z
is equal to ctr
. How can I do that?
My code:
char res;
int ctr = 10
char[] flames = {'F','L','A','M','E','S'};
for(int z = 0; z < ctr-1; z++){
res = (flames[z]);
jLabel1.setText(String.valueOf(res));
}
You need to use an index that is limited to the size of the array. More precisely, and esoterically speaking, you need to map the for-loop iterations {0..9} to the valid indexes for the flame array {0..flames.length()-1
}, which are the same, in this case, to {0..5}.
When the loop iterates from 0 to 5, the mapping is trivial. When the loop iterates a 6th time, then you need to map it back to array index 0, when it iterates to the 7th time, you map it to array index 1, and so on.
== Naïve Way ==
for(int z = 0, j = 0; z < ctr-1; z++, j++)
{
if ( j >= flames.length() )
{
j = 0; // reset back to the beginning
}
res = (flames[j]);
jLabel1.setText(String.valueOf(res));
}
== A More Appropriate Way ==
Then you can refine this by realizing flames.length()
is an invariant, which you move out of a for-loop.
final int n = flames.length();
for(int z = 0, j = 0; z < ctr-1; z++, j++)
{
if ( j >= n )
{
j = 0; // reset back to the beginning
}
res = (flames[j]);
jLabel1.setText(String.valueOf(res));
}
== How To Do It ==
Now, if you are paying attention, you can see we are simply doing modular arithmetic on the index. So, if we use the modular (%) operator, we can simplify your code:
final int n = flames.length();
for(int z = 0; z < ctr-1; z++)
{
res = (flames[z % n]);
jLabel1.setText(String.valueOf(res));
}
When working with problems like this, think about function mappings, from a Domain (in this case, for loop iterations) to a Range (valid array indices).
More importantly, work it out on paper before you even begin to code. That will take you a long way towards solving these type of elemental problems.
这篇关于回到第一个索引到达阵列中的最后一个之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!