java 矩阵[多维数组]源代码
java哥
阅读:518
2021-04-01 10:07:50
评论:0
1.import java.util.Random;
2.
3.//矩阵类
4.public class Matrix {
5.
6. private int[][] matrix;
7.
8. Random random = new Random();
9.
10. // 构造方法
11. public Matrix() {
12. matrix = new int[3][3];
13.
14. }
15.
16. public Matrix(int n) {
17. matrix = new int[n][n];
18.
19. for (int i = 0; i < n; i++) {
20. for (int j = 0; j < n; j++) {
21. matrix[i][j] = random.nextInt(100);
22. }
23. }
24. }
25.
26. public Matrix(int n, int m) {
27. matrix = new int[n][m];
28.
29. for (int i = 0; i < n; i++) {
30. for (int j = 0; j < m; j++) {
31. matrix[i][j] = random.nextInt(100);
32. }
33. }
34. }
35.
36. public int[][] getMatrix() {
37. return matrix;
38. }
39.
40. // 输出矩阵中所有元素
41. public void output() {
42. for (int i = 0; i < matrix.length; i++) {
43. for (int j = 0; j < matrix[i].length; j++) {
44. System.out.print(matrix[i][j] + "\t");
45. }
46. System.out.println();
47. }
48. }
49.
50. // 求一个矩阵的转置矩阵
51. public Matrix transpose() {
52.
53. int n = matrix.length;
54. int m = matrix[0].length;
55.
56. Matrix transMatrix = new Matrix(n, m);
57.
58. for (int i = 0; i < n; i++) {
59. for (int j = 0; j < m; j++) {
60. transMatrix.getMatrix()[i][j] = matrix[j][i];
61. }
62. }
63.
64. return transMatrix;
65. }
66.
67. // 判断一个矩阵是否为上三角矩阵
68. public boolean isTriangular() {
69.
70. // 用相反的思路进行判断
71. for (int i = 1; i < matrix.length; i++) {
72. for (int j = 0; j < i; j++) {
73. if (matrix[i][j] != 0) {
74. return false;
75. }
76. }
77. }
78.
79. return true;
80. }
81.
82. // 判断是否为对称矩阵
83. public boolean isSymmetry() {
84.
85. for (int i = 1; i < matrix.length; i++) {
86. for (int j = 0; j < matrix[i].length; j++) {
87. if (matrix[i][j] != matrix[j][i]) {
88. return false;
89. }
90. }
91. }
92.
93. return true;
94. }
95.
96. // 矩阵的相加
97. public void add(Matrix b) {
98.
99. int[][] matrixOfB = b.getMatrix();
100.
101. int n = matrixOfB.length;
102. int m = matrixOfB[0].length;
103.
104. if (n != matrix.length || m != matrix[0].length) {
105. System.out.println("矩阵的长度不一致,不能相加");
106. } else {
107. for (int i = 0; i < n; i++) {
108. for (int j = 0; j < m; j++) {
109. matrix[i][j] += matrixOfB[i][j];
110. }
111. }
112.
113. }
114. }
115.
116. public static void main(String[] args) {
117.
118. //测试
119. Matrix test1 = new Matrix(4);
120.
121. System.out.println("原始矩阵");
122. test1.output();
123.
124. Matrix transMatrix = test1.transpose();
125.
126. System.out.println("转置矩阵");
127. transMatrix.output();
128.
129. System.out.println("是否是上三角矩阵");
130. System.out.println(test1.isTriangular());
131.
132. System.out.println("是否是对称矩阵");
133. System.out.println(test1.isSymmetry());
134.
135. System.out.println("----------------------");
136.
137. Matrix test2 = new Matrix();
138.
139. test2.output();
140.
141. System.out.println(test2.isTriangular());
142. System.out.println(test2.isSymmetry());
143.
144. System.out.println("----------------------");
145.
146. Matrix test3 = new Matrix(4);
147. Matrix test4 = new Matrix(4);
148.
149. test3.add(test2);
150.
151. System.out.println("矩阵1");
152. test3.output();
153. System.out.println("矩阵2");
154. test4.output();
155.
156. System.out.println("矩阵相加");
157. test3.add(test4);
158.
159. test3.output();
160. }
161.
162.}
声明
1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。