【发布时间】:2018-09-10 17:02:59
【问题描述】:
我有一个字符串变量,它使用 toString() 存储获取 ArrayList 的值。
matrixOne = new ArrayList<ArrayList<ArrayList<T>>>();
String output = matrixOne.toString().replace("[", "").replace("]", "");
输出打印以下值:
a, b, c, d, e, f, i, h, g
我希望它们在 toString() 方法中被格式化,以在新行上格式化为它们的实际行和列,值之间有一个制表符。示例:
3 乘 3:
a b c
d e f
i h g
注意:行列需要通过改变row和column变量来改变,即
所以output 现在是:a, b, c, d, e, f, g, h, i, j, k, l, m, , n, o, p
2 乘 3:
a b c d e f g i
j k l m n o p q
实际方法
public String toString() {
String output = matrixOne.toString().replace("[", "").replace("]", "");
return output;
}
【问题讨论】:
-
通过
new ArrayList<ArrayList<ArrayList<T>>>();,你得到了[[[value, value], [value, value]], [[value, value], [value, value]]]的结构。您如何在其中找到行和列?您不应该使用二维ArrayList而不是三个吗? -
行和列是确定何时在构造函数中创建矩阵对象.. public Matrix(int rows, int columns) { this.rows = rows; this.columns = 列; matrixOne = new ArrayList
>>(); for(int i = 0; i >()); for(int j = 0; j ()); } } } -
我正在处理你的情况,告诉我,你传递的值总是作为字符串传递的?还是你的列表是字符串?
-
其实传入的值是通过一个方法(类型其实是泛型的
: public void insert(int row, int column, T value) { matrixOne.get(row).get (列).add(值); } -
例如; nums.insert(0, 0, "a"); nums.insert(0, 1, "b"); nums.insert(0, 2, "c");
标签: java string arraylist replace format