三元组矩阵的转置java
时间: 2023-12-06 22:36:38 浏览: 71
以下是三元组矩阵的转置Java代码示例:
```java
public class SparseMatrix {
public static void main(String[] args) {
int[][] matrix = {{0, 0, 3, 0, 4}, {0, 0, 5, 7, 0}, {0, 0, 0, 0, 0}, {0, 2, 6, 0, 0}};
int row = matrix.length;
int col = matrix[0].length;
int count = 0;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (matrix[i][j] != 0) {
count++;
}
}
}
Triple[] data = new Triple[count + 1];
data[0] = new Triple(row, col, count);
int index = 1;
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (matrix[i][j] != 0) {
data[index] = new Triple(i, j, matrix[i][j]);
阅读全文