| 1 |
package com.carrotsearch.hppc.examples; |
| 2 |
|
| 3 |
import static com.carrotsearch.hppc.examples.Helpers.*; |
| 4 |
|
| 5 |
import org.junit.Test; |
| 6 |
|
| 7 |
import com.carrotsearch.hppc.ObjectArrayList; |
| 8 |
|
| 9 |
/** |
| 10 |
* Examples of how to iterate over HPPC lists with object (generic) buffers. |
| 11 |
*/ |
| 12 |
public class HppcExample_005_IteratingOverObjectBuffers { |
| 13 |
public final ObjectArrayList<Integer> list = ObjectArrayList.from(1, 1, 2, 3, 5); |
| 14 |
|
| 15 |
/** |
| 16 |
* Compared to {@link HppcExample_001_IteratingOverLists#directBufferLoop()}, |
| 17 |
* a direct buffer access for Object types is a bit trickier because the |
| 18 |
* buffer is actually Object[], not an array of the declared generic type. |
| 19 |
* |
| 20 |
* A manual cast to the generic type (for every element) is required. |
| 21 |
*/ |
| 22 |
@Test |
| 23 |
public void directBufferLoop() throws Exception { |
| 24 |
final Object[] buffer = list.buffer; |
| 25 |
final int size = list.size(); |
| 26 |
for (int i = 0; i < size; i++) { |
| 27 |
printfln("list[%d] = %d", i, buffer[i]); |
| 28 |
} |
| 29 |
} |
| 30 |
} |