整形的数组小工具
1 package algorithms.self.tools; 2 3 import java.util.Arrays; 4 5 /** 6 * This is a class to implement Integer array tools 7 * 8 * @author ygh 2017年3月14日 9 */ 10 public class IntegerArrayTools { 11 12 /** 13 * Print a int[] into console 14 * 15 * @param arr The array need to print 16 */ 17 public static void toStringIntArray(int[] arr) { 18 System.out.println(Arrays.toString(arr)); 19 } 20 21 /** 22 * Print a int[][] into console 23 * 24 * @param arr The array need to print 25 */ 26 public static void toTwoDimensionalString(int[][] arr) { 27 int row = arr.length; 28 for (int i = 0; i < row; i++) { 29 toStringIntArray(arr[i]); 30 } 31 } 32 33 /** 34 * Get a random array by size and max value 35 * 36 * @param size The size the new array you want to create 37 * @param maxValue The max value in the array; 38 * @return A random array 39 */ 40 public static int[] getRandomArray(int size, int maxValue) { 41 int[] arr = new int[size]; 42 for (int i = 0; i < size; i++) { 43 arr[i] = getIntRandomValue(maxValue); 44 } 45 return arr; 46 } 47 48 /** 49 * Get a random that less than max value 50 * 51 * @param maxValue The max value you get the random number 52 * @return A random number less than max value 53 */ 54 public static int getIntRandomValue(int maxValue) { 55 return (int) (Math.random() * maxValue); 56 } 57 58 /** 59 * Get a random value in a integer array 60 * 61 * @param arr A value come from this array 62 * @return A random value comes from this array 63 */ 64 public static int getRandomValue(int[] arr) { 65 int length = arr.length; 66 return (arr[getIntRandomValue(length)]); 67 } 68 69 /** 70 * Get the max value in a Array. 71 * 72 * @param arr[] The max value come from 73 * @return The max value in this array 74 */ 75 public static int getMaxValue(int arr[]) { 76 int max = arr[0]; 77 for (int i = 1; i < arr.length - 1; i++) { 78 if (max < arr[i]) { 79 max = arr[i]; 80 } 81 } 82 return max; 83 } 84 85 /** 86 * Get a two-dimensional integer array 87 * 88 * @param row The size of row 89 * @param column The size of column 90 * @param maxValue The max value in this array 91 * @return a new two-dimensional integer array 92 */ 93 public static int[][] getRandomTwoDimensionalArray(int row, int column, int maxValue) { 94 int[][] arr = new int[row][column]; 95 for (int i = 0; i < row; i++) { 96 arr[i] = getRandomArray(column, maxValue); 97 } 98 99 return arr; 100 } 101 102 /** 103 * Get average of an integer array 104 * 105 * @param arr The array provide data 106 * @return The average of this array 107 */ 108 public static double getArrayAverage(int[] arr) { 109 int length = arr.length; 110 int sum = 0; 111 for (int i = 0; i < length; i++) { 112 sum += arr[i]; 113 } 114 return (sum / length); 115 } 116 117 /** 118 * Copy an array to new array 119 * 120 * @param arr The array needed to copy 121 * @return The copied array 122 */ 123 public static int[] copy(int arr[]) { 124 int length = arr.length; 125 int newArr[] = new int[length]; 126 for (int i = 0; i < length; i++) { 127 newArr[i] = arr[i]; 128 } 129 return newArr; 130 } 131 132 /** 133 * Reverse the elements within an array 134 * 135 * @param arr An array want to reverse all elements 136 */ 137 public static void reverse(int[] arr) { 138 int tmp; 139 int length = arr.length; 140 for (int i = 0; i < length / 2; i++) { 141 tmp = arr[i]; 142 arr[i] = arr[length - 1 - i]; 143 arr[length - 1 - i] = tmp; 144 } 145 } 146 147 /** 148 * Transpose a two-dimensional square array 149 * 150 * @param arr 151 */ 152 public static void transposition(int[][] arr) { 153 int row = arr.length; 154 int temp; 155 for (int i = 0; i < row; i++) { 156 for (int j = 0; j < i; j++) { 157 temp = arr[i][j]; 158 arr[i][j] = arr[j][i]; 159 arr[j][i] = temp; 160 } 161 } 162 } 163 164 /** 165 * Get a integer array sum 166 * @param arr The array to provide the data 167 * @return The sum of all elements in the array 168 */ 169 public static int getIntegrArraySum(int arr[]) { 170 int sum = 0; 171 for (int i = 0; i < arr.length; i++) { 172 sum = sum + arr[i]; 173 } 174 return sum; 175 } 176 177 }