Advertisement
MrPolywhirl

MatrixUtils.java

Nov 1st, 2014
471
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.09 KB | None | 0 0
  1. public class MatrixUtils {
  2.        public static void main(String[] args) {
  3.               int[][] matrix = null;
  4.  
  5.               matrix = new int[][] { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
  6.  
  7.               System.out.println(formatMatrix(matrix, "=", "-") + '\n');
  8.               System.out.println(formatMatrix(matrix) + '\n');
  9.               System.out.println(formatMatrix(resize(matrix, 4, 4)) + '\n');
  10.               System.out.println(formatMatrix(resize(matrix, 2, 2)) + '\n');
  11.               System.out.println(formatMatrix(resize(matrix, 10, 15)) + '\n');
  12.        }
  13.  
  14.        public static final int getWidth(final int[][] matrix) {
  15.               int max = 0;
  16.               if (matrix.length > 0) {
  17.                      max = matrix[0].length;
  18.                      if (matrix.length > 1) {
  19.                            for (int i = 1; i < matrix.length; i++) {
  20.                                   if (matrix[i].length > max) {
  21.                                          max = matrix[i].length;
  22.                                   }
  23.                            }
  24.                      }
  25.               }
  26.               return max;
  27.        }
  28.  
  29.        public static final int getHeight(final int[][] matrix) {
  30.               return matrix.length;
  31.        }
  32.  
  33.        public static int[][] resize(final int[][] matrix, final int w, final int h) {
  34.               int width = getWidth(matrix);
  35.               int height = getHeight(matrix);
  36.               return addRows(addCols(matrix, w - width), h - height);
  37.        }
  38.  
  39.        private static int[][] addRows(final int[][] matrix, final int n) {
  40.               if (n == 0) {
  41.                      return matrix;
  42.               }
  43.               int oldHeight = matrix.length;
  44.               int newHeight = oldHeight + n;
  45.               int width = getWidth(matrix);
  46.               int[][] copy = new int[newHeight][];
  47.               System.arraycopy(matrix, 0, copy, 0, n > 0 ? oldHeight : newHeight);
  48.               for (int i = oldHeight; i < newHeight; i++) {
  49.                      copy[i] = new int[width];
  50.               }
  51.               return copy;
  52.        }
  53.  
  54.        private static int[][] addCols(final int[][] matrix, final int n) {
  55.               if (n == 0) {
  56.                      return matrix;
  57.               }
  58.               int oldWidth = getWidth(matrix);
  59.               int newWidth = oldWidth + n;
  60.               int height = matrix.length;
  61.               int[][] copy = new int[height][newWidth];
  62.               for (int i = 0; i < height; i++) {
  63.                      copy[i] = new int[newWidth];
  64.                      System.arraycopy(matrix[i], 0, copy[i], 0, n > 0 ? oldWidth
  65.                                   : newWidth);
  66.               }
  67.               return copy;
  68.        }
  69.  
  70.        public static String formatMatrix(final int[][] matrix) {
  71.               return formatMatrix(matrix, "\n", ",").toString();
  72.        }
  73.  
  74.  
  75.        public static String formatMatrix(final int[][] matrix, String vSep, String hSep) {
  76.               return join(new StringBuffer(), matrix, vSep, hSep).toString();
  77.        }
  78.  
  79.        public static StringBuffer join(final int[][] arr, String vSep, String hSep) {
  80.               return join(new StringBuffer(), arr, vSep, hSep);
  81.        }
  82.  
  83.        public static StringBuffer join(final int[] arr, String sep) {
  84.               return join(new StringBuffer(), arr, sep);
  85.        }
  86.  
  87.        protected static StringBuffer join(StringBuffer buff, final int[][] arr, String vSep, String hSep) {
  88.               if (arr.length > 0) {
  89.                      join(buff, arr[0], hSep);
  90.                      for (int i = 1; i < arr.length; i++) {
  91.                            join(buff.append(vSep), arr[i], hSep);
  92.                      }
  93.               }
  94.               return buff;
  95.        }
  96.  
  97.        protected static StringBuffer join(StringBuffer buff, int[] arr, String sep) {
  98.               if (arr.length > 0) {
  99.                      buff.append(arr[0]);
  100.                      for (int i = 1; i < arr.length; i++) {
  101.                            buff.append(sep).append(arr[i]);
  102.                      }
  103.               }
  104.               return buff;
  105.        }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement