-public class ArrayUtils {\r
- \r
- /**\r
- * Drop elements from array \r
- * \r
- * @param src source array\r
- * @param elementsToDrop\r
- * @return src or new array \r
- */\r
- @SafeVarargs\r
- public static <T> T[] dropElements(T src[], T...elementsToDrop)\r
- {\r
- if (src.length==0) return src;\r
- int count = 0;\r
- nextA:\r
- for (T a : src) {\r
- for (T b : elementsToDrop)\r
- if (a==b) continue nextA;\r
- count++;\r
- }\r
- if (count==src.length) return src;\r
- \r
- Class<?> componentClass = src.getClass().getComponentType();\r
- @SuppressWarnings("unchecked")\r
- T[] result = (T[]) Array.newInstance(componentClass, count);\r
- if (count==0) return result;\r
- int index = 0;\r
- nextA2:\r
- for (T a : src) {\r
- for (T b : elementsToDrop)\r
- if (a==b) continue nextA2;\r
- result[index++] = a;\r
- }\r
- return result;\r
- } \r
- \r
- /**\r
- * Replace first occurance of object with another\r
- * \r
- * @param src\r
- * @param from\r
- * @param to\r
- */\r
- public static void replaceFirst(Object src[], Object from, Object to) {\r
- for (int i=0; i<src.length; i++)\r
- if (src[i] == from) {\r
- src[i] = to;\r
- return;\r
- }\r
- }\r
- \r
- /**\r
- * Concatenate two arrays\r
- * @param a\r
- * @param b\r
- * @return A new array with elements of a followed by elements of b\r
- */\r
- public static <T> T[] concatenate(T[] a, T[] b) {\r
- @SuppressWarnings("unchecked")\r
- Class<? extends T> compType = (Class<? extends T>) getCommonBase(a.getClass().getComponentType(), b.getClass().getComponentType());\r
- if (compType == null)\r
- throw new RuntimeException("Could not deduce common array type for " + a.getClass().getName() + " and " + b.getClass().getName());\r
- return concatenate(a, b, compType);\r
- }\r
- \r
- /**\r
- * Concatenate two arrays\r
- * @param a\r
- * @param b\r
- * @param componentType The actual component type of the created array\r
- * @return A new array with elements of a followed by elements of b\r
- */\r
- public static <T> T[] concatenate(T[] a, T[] b, Class<? extends T> componentType) {\r
- @SuppressWarnings("unchecked")\r
- T[] result = (T[]) Array.newInstance(componentType, a.length + b.length);\r
- System.arraycopy(a, 0, result, 0, a.length);\r
- System.arraycopy(b, 0, result, a.length, b.length);\r
- return result;\r
- }\r
- \r
- /**\r
- * Concatenate two arrays, cropping or extending the result to a given length\r
- * @param a\r
- * @param b\r
- * @param length Length of the resulting array\r
- * @return A new array with elements of a followed by elements of b, possibly padded with nulls to reach a given length\r
- */\r
- public static <T> T[] concatenate(T[] a, T[] b, int length) {\r
- @SuppressWarnings("unchecked")\r
- Class<? extends T> compType = (Class<? extends T>)getCommonBase(a.getClass().getComponentType(), b.getClass().getComponentType());\r
- if (compType == null)\r
- throw new RuntimeException("Could not deduce common array type for " + a.getClass().getName() + " and " + b.getClass().getName());\r
- return concatenate(a, b, length, compType);\r
- }\r
- \r
- /**\r
- * Concatenate two arrays, cropping or extending the result to a given length\r
- * @param a\r
- * @param b\r
- * @param length Length of the resulting array\r
- * @param componentType The actual component type of the created array\r
- * @return A new array with elements of a followed by elements of b, possibly padded with nulls to reach a given length\r
- */\r
- public static <T> T[] concatenate(T[] a, T[] b, int length, Class<? extends T> componentType) {\r
- @SuppressWarnings("unchecked")\r
- T[] result = (T[]) Array.newInstance(componentType, length);\r
- System.arraycopy(a, 0, result, 0, Math.min(length, a.length));\r
- if (length > a.length)\r
- System.arraycopy(b, 0, result, a.length, Math.min(b.length, length - a.length));\r
- return result;\r
- }\r
- \r
- /**\r
- * Add new elements to the end of an array\r
- * @param a\r
- * @param b\r
- * @return A new array with elements b appended at the end\r
- */\r
- @SafeVarargs\r
- public static <T> T[] append(T[] a, T ... b) {\r
- return concatenate(a, b);\r
- }\r
+import java.lang.reflect.Array;
+
+public class ArrayUtils {
+
+ /**
+ * Drop elements from array
+ *
+ * @param src source array
+ * @param elementsToDrop
+ * @return src or new array
+ */
+ @SafeVarargs
+ public static <T> T[] dropElements(T src[], T...elementsToDrop)
+ {
+ if (src.length==0) return src;
+ int count = 0;
+ nextA:
+ for (T a : src) {
+ for (T b : elementsToDrop)
+ if (a==b) continue nextA;
+ count++;
+ }
+ if (count==src.length) return src;
+
+ Class<?> componentClass = src.getClass().getComponentType();
+ @SuppressWarnings("unchecked")
+ T[] result = (T[]) Array.newInstance(componentClass, count);
+ if (count==0) return result;
+ int index = 0;
+ nextA2:
+ for (T a : src) {
+ for (T b : elementsToDrop)
+ if (a==b) continue nextA2;
+ result[index++] = a;
+ }
+ return result;
+ }
+
+ /**
+ * Replace first occurance of object with another
+ *
+ * @param src
+ * @param from
+ * @param to
+ */
+ public static void replaceFirst(Object src[], Object from, Object to) {
+ for (int i=0; i<src.length; i++)
+ if (src[i] == from) {
+ src[i] = to;
+ return;
+ }
+ }
+
+ /**
+ * Concatenate two arrays
+ * @param a
+ * @param b
+ * @return A new array with elements of a followed by elements of b
+ */
+ public static <T> T[] concatenate(T[] a, T[] b) {
+ @SuppressWarnings("unchecked")
+ Class<? extends T> compType = (Class<? extends T>) getCommonBase(a.getClass().getComponentType(), b.getClass().getComponentType());
+ if (compType == null)
+ throw new RuntimeException("Could not deduce common array type for " + a.getClass().getName() + " and " + b.getClass().getName());
+ return concatenate(a, b, compType);
+ }
+
+ /**
+ * Concatenate two arrays
+ * @param a
+ * @param b
+ * @param componentType The actual component type of the created array
+ * @return A new array with elements of a followed by elements of b
+ */
+ public static <T> T[] concatenate(T[] a, T[] b, Class<? extends T> componentType) {
+ @SuppressWarnings("unchecked")
+ T[] result = (T[]) Array.newInstance(componentType, a.length + b.length);
+ System.arraycopy(a, 0, result, 0, a.length);
+ System.arraycopy(b, 0, result, a.length, b.length);
+ return result;
+ }
+
+ /**
+ * Concatenate two arrays, cropping or extending the result to a given length
+ * @param a
+ * @param b
+ * @param length Length of the resulting array
+ * @return A new array with elements of a followed by elements of b, possibly padded with nulls to reach a given length
+ */
+ public static <T> T[] concatenate(T[] a, T[] b, int length) {
+ @SuppressWarnings("unchecked")
+ Class<? extends T> compType = (Class<? extends T>)getCommonBase(a.getClass().getComponentType(), b.getClass().getComponentType());
+ if (compType == null)
+ throw new RuntimeException("Could not deduce common array type for " + a.getClass().getName() + " and " + b.getClass().getName());
+ return concatenate(a, b, length, compType);
+ }
+
+ /**
+ * Concatenate two arrays, cropping or extending the result to a given length
+ * @param a
+ * @param b
+ * @param length Length of the resulting array
+ * @param componentType The actual component type of the created array
+ * @return A new array with elements of a followed by elements of b, possibly padded with nulls to reach a given length
+ */
+ public static <T> T[] concatenate(T[] a, T[] b, int length, Class<? extends T> componentType) {
+ @SuppressWarnings("unchecked")
+ T[] result = (T[]) Array.newInstance(componentType, length);
+ System.arraycopy(a, 0, result, 0, Math.min(length, a.length));
+ if (length > a.length)
+ System.arraycopy(b, 0, result, a.length, Math.min(b.length, length - a.length));
+ return result;
+ }
+
+ /**
+ * Add new elements to the end of an array
+ * @param a
+ * @param b
+ * @return A new array with elements b appended at the end
+ */
+ @SafeVarargs
+ public static <T> T[] append(T[] a, T ... b) {
+ return concatenate(a, b);
+ }
+
+ /**
+ * Add new elements to the end of an array
+ * @param a
+ * @param b
+ * @return A new array with elements b appended at the end
+ */
+ @SafeVarargs
+ public static <T> T[] append(Class<? extends T> compType, T[] a, T ... b) {
+ return concatenate(a, b, compType);
+ }
+
+ /**
+ * Add new elements to the end of an array
+ * @param a
+ * @param b
+ * @param length Length of the resulting array
+ * @return A new array with elements b appended at the end
+ */
+ @SafeVarargs
+ public static <T> T[] append(int length, T[] a, T ... b) {
+ return concatenate(a, b, length);
+ }
+
+ /**
+ * Add new elements to the end of an array
+ * @param a
+ * @param b
+ * @param length Length of the resulting array
+ * @return A new array with elements b appended at the end
+ */
+ @SafeVarargs
+ public static <T> T[] append(Class<? extends T> compType, int length, T[] a, T ... b) {
+ return concatenate(a, b, length, compType);
+ }
+
+ /**
+ * Get an array with elements from a source array at given indices
+ * @param source A source array
+ * @param index An index array
+ * @return A new array with element i equal to source[index[i]]
+ */
+ public static <T> T[] indirection(T[] source, int[] index) {
+ @SuppressWarnings("unchecked")
+ T[] result = (T[]) Array.newInstance(source.getClass().getComponentType(), index.length);
+ for (int i = 0; i < index.length; i++)
+ result[i] = source[index[i]];
+ return result;
+ }
+
+ /**
+ * Get an array with given length with elements from a source array at given indices
+ * @param source A source array
+ * @param index An index array
+ * @return A new array with element i equal to source[index[i]]. The resulting array length is set to #length.
+ */
+ public static <T> T[] indirection(T[] source, int[] index, int length) {
+ @SuppressWarnings("unchecked")
+ T[] result = (T[]) Array.newInstance(source.getClass().getComponentType(), length);
+ int n = Math.min(length, index.length);
+ for (int i = 0; i < n; i++)
+ result[i] = source[index[i]];
+ return result;
+ }