diagram-system / DIAGRAM SYSTEM / src / system / TwoDimArrayUtil.java
TwoDimArrayUtil.java
Raw
package system;

public class TwoDimArrayUtil {

	public static void main(String[] args) {
	}

	// CONSTRUCTOR
	public TwoDimArrayUtil() {
	}

	// METHODS

	public static void arrayCopier(char[][] firstArray, char[][] secondArray,
			int row, int column) {

		for (int i = 0; i < secondArray.length; i++) {

			for (int j = 0; j < secondArray[i].length; j++) {

				firstArray[row + i][column + j] = secondArray[i][j];

			}
		}
	}

	public static char[][] appendLeftRight(char[][] left, char[][] right) {

		int numberOfRows;

		if (left == null || right == null) {
			throw new IllegalArgumentException("Error: array is null");
			// garbage in garbage out
		}

		if (right.length > left.length) {

			numberOfRows = right.length;

		} else {

			numberOfRows = left.length;
		}

		char[][] new2DArray = new char[numberOfRows][];
		
		for (int row = 0; row < numberOfRows; row++) {
			new2DArray[row] = new char[left[row].length + right[row].length];
			
		}
		
		
		TwoDimArrayUtil.arrayCopier(new2DArray, left, 0, 0);

		TwoDimArrayUtil.arrayCopier(new2DArray, right, 0, left[0].length);

		return new2DArray;

	}

	public static boolean isRagged(char[][] array) {

		if (array == null) {

			throw new IllegalArgumentException("Error: array is null");

		}

		int arrayFirstRow = array[0].length;
		int arrayLength = array.length;

		for (int i = 1; i < arrayLength; i++) {

			if (array[i].length != arrayFirstRow) {

				return true;

			}
		}

		return false;

	}

	public static void rotateTopOneRow(char[][] array) {

		if (array == null || isRagged(array)) { // if the array is ragged or
			// null throw an exception.
			throw new IllegalArgumentException(
					"Error: the array is null or it is ragged");
		}

		if (array.length == 1) { // no processing takes place if the array only
			// has one row.
			return;
		}

		// everything is fine until this point, now for the actual operations

		for (int column = 0; column < array[0].length; column++) {

			char temporaryCharacter = array[0][column];

			for (int row = 0; row < array.length - 1; row++) {

				array[row][column] = array[row + 1][column];

			}

			array[array.length - 1][column] = temporaryCharacter;

		}
	}

	public static void rotateLeftOneColumn(char[][] array) {

		if (array == null || isRagged(array)) {

			throw new IllegalArgumentException(
					"Error: the array is null or it is ragged");

		}

		if (array[0].length == 1) {

			return;

		}

		char[] temporaryArrayForShifting = new char[array.length];

		for (int i = 0; i < array.length; i++) {
			temporaryArrayForShifting[i] = array[i][0];
		}

		for (int j = 1; j < array[0].length; j++) {
			for (int k = 0; k < array.length; k++) {
				array[k][j - 1] = array[k][j];
			}
		}

		for (int k = 0; k < array.length; k++) {
			array[k][array[0].length - 1] = temporaryArrayForShifting[k];
		}
	}

	private static int longestRow(char[][] array) { // personal method created
		// to help with
		// appendTopBottom

		int maximumRowLength = array[0].length; // declare it as the first row
		// length to begin with
		int maximumRowNumber = 0;

		for (int counter = 1; counter < array.length; counter++) {

			if (array[counter].length > maximumRowLength) {
				maximumRowLength = array[counter].length;
				maximumRowNumber = counter;
			}
		}
		return maximumRowNumber;
	}

	public static char[][] appendTopBottom(char[][] top, char[][] bottom) {

		if (top == null || bottom == null) { // garbage in garbage out check
			throw new IllegalArgumentException("Error: an array is null");
		}

		int numberOfColumns = 0;
		int longestRowOfTop = longestRow(top);
		int longestRowOfBottom = longestRow(bottom);

		if (longestRowOfTop >= longestRowOfBottom) {
			numberOfColumns = longestRowOfTop;
		} else {
			numberOfColumns = longestRowOfBottom;
		}

		int bottomLength = bottom.length;
		int topLength = top.length;

		int numberOfRows = bottomLength + topLength;

		char[][] new2DArray = new char[numberOfRows][numberOfColumns];

		for (int counter = 0; counter < top.length; counter++) {
			new2DArray[counter] = top[counter];
		}

		for (int counter = 0; counter < bottom.length; counter++) {
			new2DArray[top.length + counter] = bottom[counter];
		}
		return new2DArray;
	}

	public static String toString(char[][] inputArray) {

		String stringCreated = "";

		for (int i = 0; i < inputArray.length; i++) {
			for (int j = 0; j < inputArray[i].length; j++) {
				stringCreated += inputArray[i][j];
			}
			stringCreated += "\n";
		}
		return stringCreated;
	}

}