2015-09-04 78 views
1

我的应用程序有一个功能,可以在屏幕中列出固定数量的产品(比如5)。具有固定的最大产品数量的清单产品

int max=5; 
//when the next button is clicked 
if(start<=items) 
{ 
start=start+max; 
} 
//when the previous button is clicked 
start=start-max; 


for(int i=start;i<=start+max;i++) 
{ 
//list products process 
} 

可以有3个或17,或者可以是26 products.But该算法被设计成使得它要经过5.Which的多说为17产物负载20级的产品,其可以应用程序崩溃。我的问题是“有没有办法避免额外的产品加载”?

回答

1

只要改变上限位置:

for(int i=start;i<=start+max;i++) 
{ 
    //list products process 
} 

start + maxmin(start + max , item_count - 1),其中项目计数是项目的总数。 -1是唯一必要的,如果您使用基于0的数组索引的语言。

相关问题