このクラスの public static boolean columnValuesIncrease(int [][] t)で困っています。まずこのメソッドの役割は2次元配列の値を縦の方向で比較します。何を比較するのかというと、上から下に向かっていく列の値は大きいかどうかを確認しなければいけません。メインにあるvalid から例えると、1< 2 < 3 < 7 なのであっています。そして次の縦の列。4< 6 < 9 なのでtrue です。こんな具合に比較していくメッソッドです。上から縦の方向で値を比較するのは書けたのですが、
1, 4, 5, 10 11
2, 6, 8
3, 9, 12
7
自分が書いたこのメッソッドの場合2番目のfor ループにあるt.length がいつも4になっています。多分それが原因でうまく行ってないんだと思うのですが、どうなんでしょうか?
どうしたらうまく比べられるのでしょうか?
Java
1package week04; 2 3/** 4 * Skeleton code for an array based implementation of Young's tableau. 5 * 6 * @author Iain Hewson 7 */ 8public class TableauApp { 9 10 /** 11 * The main method is just used for testing. 12 * 13 * @param args command line arguments are not used. 14 */ 15 public static void main(String[] args) { 16 final int [] [] valid = {{1,4,5,10,11},{2,6,8},{3,9,12},{7}}; 17 System.out.println(columnValuesIncrease(valid)); 18 19 } 20 21 /** 22 * Determines whether the array passed to it is a valid tableau or not. 23 * 24 * @param t a two-dimensional array to test for tableau-ness. 25 * 26 * @return true if the parameter is a valid tableau, otherwise false 27 */ 28 public static boolean isTableau(int[][] t){ 29 return false; 30 } 31 /** 32 * Returns true if no row is longer than a preceding row, 33 * Otherwise false. 34 * @param t a two-dimensional array which represents a tableau. 35 * @return true if no row is longer than a preceding row, 36 * otherwise false. 37 */ 38 public static boolean rowLengthsDecrease(int [] [] t){ 39 boolean result = true; 40 // check if the row is not empty 41 if (t[0].length != 0){ 42 System.out.println("The two dimentional array is this"); 43 System.out.println(toString(t)); 44 int first = t[0].length;// the first arrays element. 45 int second = 0;// second longest or equal to the first element. 46 for (int i =1; i<t.length;i++){ 47 // t.length = 4 48 second = t[i].length;// 0 changed to 4 49 if(result){// if the result is still true 50 if(first >= second ){// if first is longer or same as second 51 first = second;// change first value to second value(second row length) 52 result = true; 53 }else{ 54 System.out.println("No row is longer than a preceding row"); 55 result = false; 56 } 57 }// second if 58 }// for 59 }else{ 60 result = false; 61 }// first if 62 return result; 63 }// method 64 /** 65 *returns true if no row is longer than a preceding row, otherwise false. 66 *@param t two dimentional array 67 *@return true if the integers are increasing from left to right. 68 */ 69 public static boolean rowValuesIncrease(int [] [] t){ 70 71 boolean result = true; 72 for (int i =0; i<t.length; i++){ 73 int first = t[i][0];// first elements for each array 74 int second =0; 75 for (int j = 1; j<t[i].length;j++){ 76 77 78 if (t[i][j] > first){ 79 second = t[i][j]; 80 System.out.println("second int is "+" " + second); 81 System.out.println("First int is "+" " + first); 82 System.out.println(""); 83 result = true; 84 first = second; 85 System.out.println("Current result " + "" + result); 86 }else{ 87 result = false; 88 return false; 89 }// if end 90 91 }// end for loop 92 }// end for loop 93 return result; 94 95 96 }// end method 97 98 /** 99 * Returns true if from top to bottom in any column, 100 * the integers are increasing otherwise false. 101 * @param t two dimentional array 102 * @return true if the integers are increasing 103 * from top to bottom in any column. 104 */ 105 public static boolean columnValuesIncrease(int [][] t){ 106 // first integer in the two dimentional array 107 boolean result = true; 108 for (int j =0; j<t[j].length; j++;){ 109 for (int i = 1; i<t.length; i++){ 110 if (t[i][j] < first){ 111 result = false; 112 return result; 113 }else{ 114 first = t[i][j]; 115 } 116 } 117 } 118 return result; 119 120 121 122 123 } 124 125public static boolean isSetOf1toN (int [] [] t){ 126 boolean result = true; 127 // count the number of cells in the array 128 int count = 0;// 10 129 // get total number of cells 130 for (int i = 0; i<t.length; i++){ 131 count += t[i].length; 132 133 } 134 // get the last number in the array 135 136 int n = t.length-2;//2 137 int f = t[n][t[n].length-1]; 138 System.out.println(n); 139 if (f != count ){ 140 result = false; 141 142 } 143 144 return result; 145 146 147 } 148 /** 149 * Returns a string representation of an array based tableau. 150 * 151 * @param t a two-dimensional array which represents a tableau. 152 * 153 * @return a string representation of an array based tableau. 154 */ 155 public static String toString(int[][] t) { 156 StringBuilder result = new StringBuilder(); 157 for (int i = 0; i < t.length; i++) { 158 for (int j = 0; j < t[i].length; j++) { 159 result.append(String.format("%-4s", t[i][j])); 160 } 161 if (i < t.length-1) { 162 result.append("\n"); 163 } 164 } 165 return result.toString(); 166 } 167 168} 169
回答5件
あなたの回答
tips
プレビュー