2015-09-04 73 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的數組索引的語言。

相關問題