@Nonnull public <T> T[] toArray(@Nonnull T[] a) { final int s = size(); if (s > a.length) { a = Arrays.copyOf(a, s); } else if (s < a.length) { a[s] = null; } int i = 0; for (ConsList c = this;; c = (ConsList) c.rest) { //noinspection unchecked a[i++] = (T) c.first; if (!(c.rest instanceof ConsList)) { Object[] a2 = c.rest.toArray(); //noinspection SuspiciousSystemArraycopy System.arraycopy(a2, 0, a, i, a2.length); return a; } } }
@Nonnull public <T> T[] toArray(@Nonnull T[] a) { final int s = size(); if (s > a.length) { a = Arrays.copyOf(a, s); } else if (s < a.length) { a[s] = null; } int i = 0; for (ConsList c = this;; c = (ConsList) c.rest) { //noinspection unchecked a[i++] = (T) c.first; if (!(c.rest instanceof ConsList)) { Object[] a2 = c.rest.toArray(); //noinspection SuspiciousSystemArraycopy System.arraycopy(a2, 0, a, i, a2.length); return a; } } }