1 5 package com.tctest.performance.http.load; 6 7 import java.util.ArrayList ; 8 import java.util.List ; 9 10 13 public class IntList { 14 private static final int BLOCK = 4096; 15 private final List arrays = new ArrayList (); 16 private int[] current; 17 private int currentIndex = 0; 18 private int size; 19 20 public IntList() { 21 next(); 22 } 23 24 public void add(int i) { 25 if (currentIndex == current.length) { 26 next(); 27 } 28 29 current[currentIndex++] = i; 30 size++; 31 } 32 33 public int size() { 34 return size; 35 } 36 37 public int get(int index) { 38 int whichArray = index == 0 ? 0 : index / BLOCK; 39 return ((int[]) arrays.get(whichArray))[index % BLOCK]; 40 } 41 42 private void next() { 43 current = new int[BLOCK]; 44 currentIndex = 0; 45 arrays.add(current); 46 } 47 48 public static void main(String args[]) { 49 int num = 5000000; 50 IntList il = new IntList(); 51 52 for (int i = 0; i < num; i++) { 53 il.add(i); 54 55 } 56 57 if (il.size() != num) { throw new AssertionError ("wrong size reported: " + il.size()); } 58 59 for (int i = 0; i < num; i++) { 60 int val = il.get(i); 61 if (val != i) { throw new AssertionError ("Expected " + i + " got " + val); } 62 } 63 } 64 65 } 66 | Popular Tags |