通过阵列的Java循环 [英] Java loop through array
本文介绍了通过阵列的Java循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里有这个循环
for(int i =0; i < prices.length; i++)
{
if(prices[i]>largest)
{
largest = prices[i];
}
else if(prices[i]<smallest)
{
smallest= prices[i];
}
}
这遍历整个数组,发现最小和最大的价值。说我通过第一20个元素我怎么做,想只循环?我已经把沿线嵌套循环下for循环,看到的线尝试,如果我遇到它,但我做不到。
which loops through the whole array and finds the min and max value. Say I wanted to only loop through the first 20 elements how do I do that? I have tried along the lines of putting a nested loop in under this for loop and seeing if I come across it but I can't.
推荐答案
您只需要添加的要求,循环控制条件:
You could just add the requirement to the loop control condition:
for(int i =0; i < prices.length && i < 20; i++)
这将检查的第20个元素,其中疗法均超过20阵列中,但整个数组,如果有少于20项。
This would check the first 20 elements where ther are more than 20 in the array, but the whole array if there are less than 20 items.
这篇关于通过阵列的Java循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文