资源简介
1.方便获得一个字符串表示的矩阵
2.删除二维数组中的第几行
3.删除二维数组中与所要删除行内容一样的此行
4.获得此二维数组
代码片段和文件信息
import java.util.ArrayList;
import java.util.List;
public class Matrix {
private String[][] data;
public Matrix(String[][] data) {
int r = data.length;
int c = data[0].length;
this.data = new String[r][c];
for (int i = 0; i < r; i++) {
for (int j = 0; j < c; j++) {
this.data[i][j] = data[i][j];
}
}
}
/*
* convenience method for getting a string representation of matrix
*/
public String toString() {
StringBuilder sb = new StringBuilder(1024);
for (String[] row : this.data) {
for (String val : row) {
sb.append(val);
sb.append(“ “);
}
sb.append(“\n“);
}
return (sb.toString());
}
public void removeRow(final int index) {
/*
* Use an array list to track of the rows we‘re going to want to
* keep...arraylist makes it easy to grow dynamically so we don‘t need
* to know up front how many rows we‘re keeping
*/
List rowsToKeep = new ArrayList(this.data.length);
for (String[] row : this.data) {
rowsToKeep.add(row);
}
rowsToKeep.remove(index);
/*
* now that we know what rows we want to keep make our new 2D array
* with only those rows
*/
this.data = new String[rowsToKeep.size()][];
for (int i = 0; i < rowsToKee
- 上一篇:闹钟源代码
- 下一篇:jdk-8u201-windows-x64
评论
共有 条评论