A semantic search engine for source code https://bitshift.benkurtovic.com/
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Matrix.java 6.5 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package battlechap;
  2. import java.io.PrintStream;
  3. public class Matrix {
  4. private Object[][] _datmatrix;
  5. public Matrix(int paramInt){
  6. this._datmatrix = new Object[paramInt][paramInt];
  7. }
  8. public int size() {
  9. return this._datmatrix.length;
  10. }
  11. public Object get(int paramInt1, int paramInt2) {
  12. return this._datmatrix[paramInt1][paramInt2];
  13. }
  14. public boolean isEmpty(int paramInt1, int paramInt2) {
  15. return this._datmatrix[paramInt1][paramInt2] == null;
  16. }
  17. public boolean equals(Object paramObject) {
  18. boolean bool = true;
  19. if ((paramObject instanceof Matrix)) {
  20. Matrix localMatrix = (Matrix)paramObject;
  21. if (localMatrix.size() == size()) {
  22. for (int i = 0; i < size(); i++) {
  23. for (int j = 0; j < size(); j++) {
  24. if (!localMatrix.get(i, j).equals(get(i, j))) {
  25. bool = false;
  26. break;
  27. }
  28. }
  29. if (!bool)
  30. break;
  31. }
  32. }
  33. else
  34. bool = false;
  35. }
  36. else
  37. {
  38. bool = false;
  39. }
  40. return bool;
  41. }
  42. public Object set(int paramInt1, int paramInt2, Object paramObject) {
  43. Object localObject = this._datmatrix[paramInt1][paramInt2];
  44. this._datmatrix[paramInt1][paramInt2] = paramObject;
  45. return localObject;
  46. }
  47. public void transpose() {
  48. int i = 0;
  49. for (int j = 0; j < size(); j++) {
  50. for (int k = i; k < size(); k++) {
  51. set(j, k, set(k, j, get(j, k)));
  52. }
  53. i++;
  54. }
  55. }
  56. public static void swapRows(int paramInt1, int paramInt2, Object[][] paramArrayOfObject) {
  57. for (int i = 0; i < paramArrayOfObject[paramInt1].length; i++) {
  58. Object localObject = paramArrayOfObject[paramInt1][i];
  59. paramArrayOfObject[paramInt1][i] = paramArrayOfObject[paramInt2][i];
  60. paramArrayOfObject[paramInt2][i] = localObject;
  61. }
  62. }
  63. public static void swapCols(int paramInt1, int paramInt2, Object[][] paramArrayOfObject) {
  64. for (int i = 0; i < paramArrayOfObject.length; i++) {
  65. Object localObject = paramArrayOfObject[i][paramInt1];
  66. paramArrayOfObject[i][paramInt1] = paramArrayOfObject[i][paramInt2];
  67. paramArrayOfObject[i][paramInt2] = localObject;
  68. }
  69. }
  70. public Object[] getRow(int paramInt) {
  71. Object[] arrayOfObject = new Object[this._datmatrix[paramInt].length];
  72. for (int i = 0; i < arrayOfObject.length; i++) {
  73. arrayOfObject[i] = this._datmatrix[paramInt][i];
  74. }
  75. return arrayOfObject;
  76. }
  77. public Object[] getCol(int paramInt) {
  78. Object[] arrayOfObject = new Object[this._datmatrix[paramInt].length];
  79. for (int i = 0; i < arrayOfObject.length; i++) {
  80. arrayOfObject[i] = this._datmatrix[i][paramInt];
  81. }
  82. return arrayOfObject;
  83. }
  84. public Object[] setRow(int paramInt, Object[] paramArrayOfObject) {
  85. Object[] arrayOfObject = getRow(paramInt);
  86. for (int i = 0; i < size(); i++) {
  87. set(paramInt, i, paramArrayOfObject[i]);
  88. }
  89. return arrayOfObject;
  90. }
  91. public Object[] setCol(int paramInt, Object[] paramArrayOfObject) {
  92. Object[] arrayOfObject = getCol(paramInt);
  93. for (int i = 0; i < size(); i++) {
  94. set(i, paramInt, paramArrayOfObject[i]);
  95. }
  96. return arrayOfObject;
  97. }
  98. public String toString()
  99. {
  100. String str1 = "";
  101. for (int i = 0; i < this._datmatrix.length; i++) {
  102. if (i < 9)
  103. str1 = str1 + (i + 1) + ": ";
  104. else
  105. str1 = str1 + (i + 1) + ":";
  106. for (int j = 0; j < this._datmatrix[i].length; j++) {
  107. int k = (this._datmatrix[i][j] + "").length();
  108. String str2 = " ".substring(k);
  109. str1 = str1 + this._datmatrix[i][j] + str2;
  110. }
  111. str1 = str1 + "\n";
  112. }
  113. return str1;
  114. }
  115. public static void print(Object[][] paramArrayOfObject) {
  116. for (int i = 0; i < paramArrayOfObject.length; i++) {
  117. for (int j = 0; j < paramArrayOfObject[i].length; j++) {
  118. int k = (paramArrayOfObject[i][j] + "").length();
  119. String str = " ".substring(k);
  120. System.out.print(paramArrayOfObject[i][j] + str);
  121. }
  122. System.out.print("\n");
  123. }
  124. }
  125. public static void printArray(Object[] paramArrayOfObject) {
  126. for (int i = 0; i < paramArrayOfObject.length; i++) {
  127. int j = (paramArrayOfObject[i] + "").length();
  128. String str = " ".substring(j);
  129. System.out.print(paramArrayOfObject[i] + str);
  130. }
  131. System.out.print("\n");
  132. }
  133. public static void main(String[] paramArrayOfString) {
  134. Matrix localMatrix1 = new Matrix(5);
  135. Matrix localMatrix2 = new Matrix(5);
  136. for (int i = 0; i < localMatrix1.size(); i++) {
  137. for (int j = 0; j < localMatrix1.size(); j++) {
  138. Integer localInteger1 = new Integer((int)(Math.random() * 20.0D));
  139. localMatrix1.set(i, j, localInteger1);
  140. localMatrix2.set(i, j, localInteger1);
  141. }
  142. }
  143. System.out.println("\nDemonstrating equals method (should be true)\t" + localMatrix2.equals(localMatrix1) + "\n");
  144. System.out.println("Demonstrating get method\n" + localMatrix1.get(0, 0) + "\n");
  145. System.out.println("Demonstrating is empty method\n" + localMatrix1.isEmpty(1, 0) + "\n");
  146. System.out.println("Demonstrating size method \n" + localMatrix1.size() + "\n");
  147. System.out.println("Demonstrating toString method\n" + localMatrix1 + "\n");
  148. localMatrix1.transpose();
  149. System.out.println("Blop has been transposed\n" + localMatrix1 + "\n");
  150. Object[][] arrayOfObject = new Object[4][4];
  151. for (int j = 0; j < arrayOfObject.length; j++) {
  152. for (int k = 0; k < arrayOfObject[j].length; k++) {
  153. Integer localInteger2 = new Integer((int)(Math.random() * 20.0D));
  154. arrayOfObject[j][k] = localInteger2;
  155. }
  156. }
  157. System.out.println("\n\n**Swapping Rows Demo**");
  158. print(arrayOfObject);
  159. System.out.println("\nRows 1 and 2 have been Swapped \n");
  160. swapRows(1, 2, arrayOfObject);
  161. print(arrayOfObject);
  162. System.out.println("\n**Swapping Columns Demo**");
  163. print(arrayOfObject);
  164. System.out.println("\n\nColumns 1 and 2 have been Swapped \n");
  165. swapCols(1, 2, arrayOfObject);
  166. print(arrayOfObject);
  167. System.out.println("\n**Getting rows demo (from blop)**");
  168. System.out.println(localMatrix1);
  169. System.out.println("\nGetting row 1\n");
  170. printArray(localMatrix1.getRow(1));
  171. System.out.println("\n**Getting cols demo (from blop)**");
  172. System.out.println(localMatrix1);
  173. System.out.println("\nGetting col 1\n");
  174. printArray(localMatrix1.getCol(1));
  175. System.out.println("\n**Demonstrating set row method**");
  176. System.out.println(localMatrix1);
  177. System.out.println("\nSwitching row 1 of blop to 1st column of blop\n");
  178. localMatrix1.setRow(1, localMatrix1.getCol(1));
  179. System.out.println(localMatrix1 + "\n");
  180. System.out.println("\n**Demonstrating set col method**");
  181. System.out.println(localMatrix1);
  182. System.out.println("\nSwitching col 1 of blop to 2nd row of blop\n");
  183. localMatrix1.setCol(1, localMatrix1.getRow(2));
  184. System.out.println(localMatrix1 + "\n");
  185. }
  186. }