public K[] toArray() { K[] result = (K[])new Object[this.size()]; int index = 0; for(K k : this) result[index++] = k; return result; }
Ok, this not works K[] result = new K[this.size()];
If you could hold class. Then:
Class claz; Test(Class m) { claz = m; } K[] toArray() { K[] array=(K[])Array.newInstance(claz,this.size()); return array; }