How can I split an array into n parts?

A nice way would be to create a generic/extension method to split any array. This is mine: /// <summary> /// Splits an array into several smaller arrays. /// </summary> /// <typeparam name=”T”>The type of the array.</typeparam> /// <param name=”array”>The array to split.</param> /// <param name=”size”>The size of the smaller arrays.</param> /// <returns>An array containing smaller … Read more

casting Arrays.asList causing exception: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

For me (using Java 1.6.0_26), the first snippet gives the same exception as the second one. The reason is that the Arrays.asList(..) method does only return a List, not necessarily an ArrayList. Because you don’t really know what kind (or implementation of) of List that method returns, your cast to ArrayList<String> is not safe. The … Read more

Is array syntax using square brackets in URL query strings valid?

The answer is not simple. The following is extracted from section 3.2.2 of RFC 3986 : A host identified by an Internet Protocol literal address, version 6 [RFC3513] or later, is distinguished by enclosing the IP literal within square brackets (“[” and “]”). This is the only place where square bracket characters are allowed in … Read more

Finding neighbours in a two-dimensional array

(pseudo-code) row_limit = count(array); if(row_limit > 0){ column_limit = count(array[0]); for(x = max(0, i-1); x <= min(i+1, row_limit); x++){ for(y = max(0, j-1); y <= min(j+1, column_limit); y++){ if(x != i || y != j){ print array[x][y]; } } } } Of course, that takes almost as many lines as the original hard-coded solution, but … Read more