Never been to DZone Snippets before?

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

About this user

Kanishk Kunal

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Double Array Manipulations

// Manipulations with double dimension arrays

   1  
   2  //equate two arrays
   3  private static boolean isEqual(char [][]arr1, char [][]arr2) {
   4  		if(arr1.length!=arr2.length) return false;
   5  		boolean res = true;
   6  		for(int i=0; i<arr1.length; i++) {
   7  			res = res & Arrays.equals(arr1[i], arr2[i]);
   8  		}
   9  		return res;
  10  	}
  11  	
  12  //rotate clockwise 90 degrees
  13  private static char [][]rotate90(char [][]arr) {
  14  	char [][]ret = new char[arr.length][arr[0].length];
  15  	for(int j=0; j<arr[0].length; j++) {
  16  		for(int i=0; i<arr.length; i++) {
  17  			ret[i][j] = arr[j][i];
  18  		}
  19  	}		
  20  	//print(ret);
  21  	return reflect(ret);
  22  }
  23  	
  24  //reflect horizontally	
  25  private static char [][]reflect(char [][]arr) {
  26  	char [][]ret = new char[arr.length][arr[0].length];
  27  	for(int i=0; i<arr.length; i++) {
  28  		for(int j=0; j<arr[0].length; j++) {
  29  			ret[i][j] = arr[i][arr[i].length-j-1];
  30  		}
  31  	}		
  32  	//print(ret);
  33  	return ret;
  34  }
  35  
  36  //prints for debugging	
  37  private static void print(char [][]arr) {
  38  	for(int i=0; i<arr.length; i++) {
  39  		System.out.println(Arrays.toString(arr[i]));
  40  	}
  41  	System.out.println();
  42  }
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS