ArrayPrac

import java.util.*; public class TwoDArrayPractice { public TwoDArrayPractice(){ int[][] testArr = { {10, 9, 8, 7}, {6, 5, 4, 3}, {2, 1, -1, 0}, {22, 31, 11, 40} }; String phrase = "stop whining"; /* void rowSwap(int[][] mat, int rowAIndex, int rowBIndex); colSwap(int[][] mat, int colAIndex, int colBIndex); String[][] k = fillRowMajor(String str, int rows, int cols); int[][] j = fillColumnMajor(int[] vals, int rows, int cols); int[][] fdu = fillDownUp(int[] vals, int rows, int cols); int[][] gr = grow(int[][] mat, int newRows, int newCols); int[][] cr = crop(int[][] mat, int startRow, int startCol, int endRow, int endCol); int[][] inv = invert(int[][] mat); */ } /** * Swaps all values in the specified 2 rows of mat. * @param mat the array * @param rowAIndex the index of a row to be swapped * @param rowBIndex the index of a row to be swapped int[][] testArr = { {10, 9, 8, 7}, {6, 5, 4, 3}, {2, 1, -1, 0} {22, 31, 11, 40} }; */ public static void rowSwap(int[][] mat, int rowAIndex, int rowBIndex) { for(int i = 0; i < mat[0].length; i++){ int temp = mat[rowAIndex][i]; mat[rowAIndex][i] = mat[rowBIndex][i]; mat[rowBIndex][i] = temp; } } /** * Swaps all values in the specified 2 columns of mat. * @param mat the array * @param colAIndex the index of a column to be swapped * @param colBIndex the index of a column to be swapped */ public static void colSwap(int[][] mat, int colAIndex, int colBIndex) { for(int i = 0; i < mat.length; i++){ int temp = mat[i][colAIndex]; mat[i][colAIndex] = mat[i][colBIndex]; mat[i][colBIndex] = temp; } } /** * Returns an array with the specified number of rows and columns * containing the characters from str in row-major order. If str.length() * is greater than rows * cols, extra characters are ignored. If * str.length() is less than rows * cols, the remaining elements in the * returned array contain null. String phrase = "stop whining"; int numRows = 3, numCols = 4; String[][] expectedResult = { {"s", "t", "o", "p"}, {" ", "w", "h", "i"}, {"n", "i", "n", "g"} }; * * @param str the string to be placed in an array * @param rows the number of rows in the array to be returned * @param cols the number of columsn in the array to be returned * @return an array containing the characters from str in row-major order */ public static String[][] fillRowMajor(String str, int rows, int cols) { int s = rows * cols; int len = str.length(); for(int i = 0; i <= (s - len); i++){ str += " "; } String[][] res = new String[rows][cols]; for(int a = 0; a < res.length; a++){ for(int b = 0; b < res[a].length; b++){ res[a][b] = str.substring(0,1); str = str.substring(1); } } return res; } /** * Returns an array containing the elements of vals in column-major order. * * Precondition: vals.length == rows * cols * * @param vals the elements * @param rows the number of rows in the array to be returned * @param cols the number of columns in the array to be returned * @return an array containing the elements of vals in column-major order */ public static int[][] fillColumnMajor(int[] vals, int rows, int cols) { int as = 0; int[][] con = new int[rows][cols]; for(int c = 0; c < con[0].length; c++){ for(int r = 0; r < con.length; r++){ con[r][c] = vals[as]; as ++; } } //System.out.println(Arrays.deepToString(constant)); return con; } /** * Returns an array with the specified number of rows and columns that * contains the elements of vals in the order specified below. Elements * from vals are placed in the array by moving down the first column, * up the second column and so on. * * For example, if vals was: * {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12} * * rows was 3 and cols was 4, * the returned array would be: * { {1, 6, 7, 12}, * {2, 5, 8, 11}, * {3, 4, 9, 10} } * * Precondition: vals.length == rows * cols * * @param vals the elements * @param rows the number of rows in the array to be returned * @param cols the number of columns in the array to be returned * @return an array containing the elements from vals in the specified order */ public static int[][] fillDownUp(int[] vals, int rows, int cols) { int arsp = 0; int[][] ord = new int[rows][cols]; for(int i = 0; i < ord[0].length; i++){ if(i % 2 == 0){ for(int r = 0; r < ord.length; r++){ ord[r][i] = vals[arsp]; arsp ++; } }else{ for(int r = ord.length - 1; r >= 0; r--){ ord[r][i] = vals[arsp]; arsp++; } } } return ord; } /** * Returns a larger array containing the same elements as the * mat. The elements from mat are read in row-major order and * appear in the new array in row-major order. * * For example, if mat was: * { {10, 9, 8, 7}, * {6, 5, 4, 3}, * {2, 1, -1, 0} } * * newRows was 4 and newCols was 5 * the returned array would be: * { {10, 9, 8, 7, 6}, * {5, 4, 3, 2, 1}, * {-1, 0, 0, 0, 0}, * {0, 0, 0, 0, 0} } * * Precondition: newRows > mat.length && newCols > mat[0].length * * @param mat the 2D array containing the original elements * @param newRows the number of rows in the new array * @param newCols the number of columns in the new array * @return a larger array containing the elements from mat in row-major order */ public static int[][] grow(int[][] mat, int newRows, int newCols) { int os = 0; int[] og = new int[(newRows * newCols)]; for(int i = 0; i < mat.length; i++){ for(int b = 0; b < mat[i].length; b++){ og[os] = mat[i][b]; os++; } } for(int i = os; i < og.length; i++){ og[os] = 0; } int os2 = 0; int[][] grow = new int[newRows][newCols]; for(int i = 0; i < grow.length; i++){ for(int b = 0; b < grow[i].length; b++){ grow[i][b] = og[os2]; os2++; } } return grow; } /** * Returns a smaller array containing the elements in the specified * range of the mat. * * For example, * * mat: * { {10, 9, 8, 7}, * {6, 5, 4, 3}, * {2, 1, -1, 0} } * * startRow: 0, startCol: 1, endRow: 1, endCol: 2 * * would yield: * { {9, 8}, * {5, 4} } * * @param mat the 2D array containing the original elements * @param startRow the first row to be kept * @param startCol the first column to be kept * @param endRow the last row to be kept * @param endCol the last column to be kept * @return a smaller array containing the specified elements */ public static int[][] crop(int[][] mat, int startRow, int startCol, int endRow, int endCol) { int rowN = endRow - startRow + 1; int colsN = endCol - startCol + 1; int[][] sm = new int[rowN][colsN]; int[] a = new int[rowN * colsN]; int s = 0; for(int r = startRow; r <= endRow; r++){ for(int c = startCol; c <= endCol; c++){ a[s] = mat[r][c]; s++; } } int s2 = 0; for(int r = 0; r < sm.length; r++){ for(int c = 0; c < sm[r].length; c++){ sm[r][c] = a[s2]; s2++; } } return sm; } /** * Returns an array containing the same elements as * mat but with the rows and columns reversed. * * For example: * * mat: * { {10, 9, 8, 7}, * {6, 5, 4, 3}, * {2, 1, -1, 0} } * * would yield: * { {10, 6, 2}, * {9, 5, 1}, * {8, 4, -1}, * {7, 3, 0} } * * @param mat the array * @return an array containing elements as described */ public static int[][] invert(int[][] mat) { int[][] inv = new int[mat[0].length][mat.length]; int[] a = new int[mat[0].length * mat.length]; int s = 0; for(int c = 0; c < mat[0].length; c++){ for(int r = 0; r < mat.length; r++){ a[s] = mat[r][c]; s++; } } int s2 = 0; for(int r = 0; r < inv.length; r++){ for(int c = 0; c < inv[0].length; c++){ inv[r][c] = a[s2]; s2++; } } //System.out.println(Arrays.deepToString(inverted)); return inv; } }

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.