2011-03-29 106 views

回答

0

您正在尋找這樣的事情,假設String[] array

int pos = 0; 
while (pos + 10 < array.length) { 
    // process array[pos] to array[pos + 9] here 
    pos += 10; 
} 
// process array[pos] to array[array.length - 1] here 
0

使用thsi循環爲每個10元:

int index = 0; 
while (index < array.length) do 
{ 
    // process 
    index = index + 10; 
} 
0
String[] arr={"h","e","l","l","o"}; 
List<String> li = Arrays.asList(arr); 
final int divideBy=2; 
for(int i=0;i<arr.length;i+=divideBy){ 
    int endIndex=Math.min(i+divideBy,arr.length); 
    System.out.println(li.subList(i,endIndex)); 
} 
0

兩個嵌套的循環:

int[] nums = new int[14]; 
    // some initialization 
    for (int i = 0; i < nums.length; i++) { 
     nums[i] = i; 
    } 
    // processing your array in chunks of ten elements 
    for (int i = 0; i < nums.length; i += 10) { 
     System.out.println("processing chunk number " + 
       (i/10 + 1) + " of at most 10 nums"); 
     for (int j = i ; j < 10 * (i + 1) && j < nums.length; j++) { 
      System.out.println(nums[j]); 
     } 
    } 

輸出是

 
processing chunk number 1 of at most 10 nums 
0 
1 
2 
3 
4 
5 
6 
7 
8 
9 
processing chunk number 2 of at most 10 nums 
10 
11 
12 
13 

我用一個int[]而不是String[],但它是相同的。