DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Kunal has posted 6 posts at DZone. View Full User Profile

Double Array Manipulations

10.25.2006
| 2034 views |
  • submit to reddit
        // Manipulations with double dimension arrays

//equate two arrays
private static boolean isEqual(char [][]arr1, char [][]arr2) {
		if(arr1.length!=arr2.length) return false;
		boolean res = true;
		for(int i=0; i<arr1.length; i++) {
			res = res & Arrays.equals(arr1[i], arr2[i]);
		}
		return res;
	}
	
//rotate clockwise 90 degrees
private static char [][]rotate90(char [][]arr) {
	char [][]ret = new char[arr.length][arr[0].length];
	for(int j=0; j<arr[0].length; j++) {
		for(int i=0; i<arr.length; i++) {
			ret[i][j] = arr[j][i];
		}
	}		
	//print(ret);
	return reflect(ret);
}
	
//reflect horizontally	
private static char [][]reflect(char [][]arr) {
	char [][]ret = new char[arr.length][arr[0].length];
	for(int i=0; i<arr.length; i++) {
		for(int j=0; j<arr[0].length; j++) {
			ret[i][j] = arr[i][arr[i].length-j-1];
		}
	}		
	//print(ret);
	return ret;
}

//prints for debugging	
private static void print(char [][]arr) {
	for(int i=0; i<arr.length; i++) {
		System.out.println(Arrays.toString(arr[i]));
	}
	System.out.println();
}