package common;

import java.util.Comparator;

public class ArrayUtils {
	public static Integer[] autoBox(int[] array) {
		Integer[] converted = new Integer[array.length];
		for (int index = 0; index < array.length; index++) {
			converted[index] = array[index];
		}

		return converted;
	}

	public static Character[] autoBox(char[] array) {
		Character[] converted = new Character[array.length];
		for (int index = 0; index < array.length; index++) {
			converted[index] = array[index];
		}

		return converted;
	}

	public static <T> String arrayToString(T[] array) {
		if (array.length == 0) {
			return "[]";
		}

		StringBuffer buffer = new StringBuffer();
		int index = 0;
		buffer.append("[");
		buffer.append(array[index] == null ? "null" : array[index].toString());
		index++;
		for (; index < array.length; index++) {
			buffer.append(", ");
			buffer.append(array[index] == null ? "null" : array[index]
					.toString());
		}
		buffer.append("]");

		return buffer.toString();
	}

	public static void swap(int[] array, int fooIndex, int barIndex) {
		int temp = array[fooIndex];
		array[fooIndex] = array[barIndex];
		array[barIndex] = temp;
	}

	public static void swap(long[] array, int fooIndex, int barIndex) {
		long temp = array[fooIndex];
		array[fooIndex] = array[barIndex];
		array[barIndex] = temp;
	}

	public static <T> void swap(T[] array, int fooIndex, int barIndex) {
		T temp = array[fooIndex];
		array[fooIndex] = array[barIndex];
		array[barIndex] = temp;
	}

	public static int findIndex(int[] array, int value) {
		for (int index = 0; index < array.length; index++) {
			if (array[index] == value) {
				return index;
			}
		}

		return -1;
	}

	public static <T> int findIndex(T[] array, T value, Comparator<T> comparator) {
		for (int index = 0; index < array.length; index++) {
			if (comparator.compare(array[index], value) == 0) {
				return index;
			}
		}

		return -1;
	}
}

@@ -982,24 +982,445 @@
 ring();%0A%09%7D%0A%0A
+%09public static %3CT%3E String arrayToString(int%5B%5D array) %7B%0A%09%09if (array.length == 0) %7B%0A%09%09%09return %22%5B%5D%22;%0A%09%09%7D%0A%0A%09%09StringBuffer buffer = new StringBuffer();%0A%09%09int index = 0;%0A%09%09buffer.append(%22%5B%22);%0A%09%09buffer.append(Integer.toString(array%5Bindex%5D));%0A%09%09index++;%0A%09%09for (; index %3C array.length; index++) %7B%0A%09%09%09buffer.append(%22, %22);%0A%09%09%09buffer.append(Integer.toString(array%5Bindex%5D));%0A%09%09%7D%0A%09%09buffer.append(%22%5D%22);%0A%0A%09%09return buffer.toString();%0A%09%7D%0A%0A
 %09public stat
@@ -2325,11 +2325,170 @@
 n -1;%0A%09%7D
+%0A%0A%09public static void swap(Integer%5B%5D%5B%5D table, int a, int b, int c, int d) %7B%0A%09%09Integer temp = table%5Ba%5D%5Bb%5D;%0A%09%09table%5Ba%5D%5Bb%5D = table%5Bc%5D%5Bd%5D;%0A%09%09table%5Bc%5D%5Bd%5D = temp;%0A%09%7D
 %0A%7D%0A

Swap function for 2D array.