【问题标题】:Remove [[ ]] from multi ArrayList (toString Method)?从多 ArrayList(toString 方法)中删除 [[ ]]?
【发布时间】:2018-04-01 01:28:24
【问题描述】:

我意识到toString方法添加了[],在这种情况下是多个括号,即[[ value ]].

我需要去掉括号,转换成字符串格式,像这样的输出...

a  b  c
e  f  g
i  h  j

当前输出...

[[a], [b], [c]]
[[d], [e], [f]]
[[i], [h], [g]]

尝试:我有一个可迭代的方法,但不确定是否需要将其用于此目的?

private ArrayList<ArrayList<ArrayList<T>>> matrixOne;

public String toString() {
    return matrixOne.toString().replace("[","").replace("]","");
}

更新这里是一个有效的可验证代码。获取此输出:

public class Matrix<T> implements Iterable {

    private int rows;
    private int columns;
    private int value;
    private ArrayList<ArrayList<ArrayList<T>>> matrixOne;


public Matrix(int rows, int columns) {
    this.rows = rows;
    this.columns = columns;

         matrixOne = new ArrayList<ArrayList<ArrayList<T>>>();
//       matrixOne = new ArrayList<ArrayList<Integer>>();

            for(int i = 0; i < rows; i++) { 
                matrixOne.add(new ArrayList<ArrayList<T>>());

                for(int j = 0; j < columns; j++) { 
                matrixOne.get(i).add(new ArrayList<T>()); 
                }
            } 

    }   

public void insert(int row, int column, T value) {
    matrixOne.get(row).get(column).add( value);
}

 // THIS METHOD!
public String toString() {

    return matrixOne.toString().replaceAll("\[\[|\]|,|\[|\]\]", "");
}

public Iterator iterator() {
      Iterator itr = matrixOne.iterator();
      return itr;
}

 public static void main(String[] args) {
        Matrix<String> nums = new Matrix(3, 3);
        nums.insert(0, 0, "a");
        nums.insert(0, 1, "b");
        nums.insert(0, 2, "c");
//
        nums.insert(1, 0, "d");
        nums.insert(1, 1, "e");
        nums.insert(1, 2, "f");
//
        nums.insert(2, 2, "g");
        nums.insert(2, 1, "h");
        nums.insert(2, 0, "i");

        for(Object nm : nums) {
            System.out.println(nm.toString());
        }

请给点意见?

【问题讨论】:

  • 你需要使用replaceAll() 否则它只会替换第一个匹配的字符
  • 为什么不直接使用循环打印字符串。
  • 发布您的minimal reproducible example 以证明问题。您的输出看起来甚至没有执行您的自定义方法。展示您如何创建和打印数据。一个简单的 replace(...) 对我有用。
  • @StephenC OP 使用的是replace,而不是replaceAll,所以不是正则表达式。
  • @Austin replace 将替换所有实例:替换 每个 子字符串 ...

标签: java string arraylist collections


【解决方案1】:

一个简单的 replace(...) 对我有用:

public class Main
{
    public static void main(String[] args) throws Exception
    {
        ArrayList<String> al1 = new ArrayList<String>();
        al1.add("a");
        al1.add("b");
        ArrayList<String> al2 = new ArrayList<String>();
        al2.add("1");
        al2.add("2");

        ArrayList<ArrayList<String>> al = new ArrayList<ArrayList<String>>();
        al.add(al1);
        al.add(al2);

        System.out.println(al);
        System.out.println( al.toString().replace("[", "").replace("]", "") );
    }
}

【讨论】:

  • 爱所有的反对票。否决的选民甚至尝试过代码吗?请解释问题是什么。这对我来说似乎是一个简单的解决方案。我错过了什么吗?
  • 不确定谁在投票给大家,但我试过你回答,它没有奏效。我已经更新了这个问题,以展示我是如何实例化这个列表的:matrixOne = new ArrayList>>(); (和你的不一样),请看一下? @camickr
  • @Shaz,你复制我的代码并执行了吗?是否删除了“[”和“]}?我的回答的重点是证明 String.replace(...) 方法有效。因此,如果对象上的 toString(...) 方法具有“[”和“]”,然后我提供的代码将删除它们。I've updated the question - 我无法复制/粘贴/编译和文本您的代码。我不知道什么是“矩阵”。那不是一部分JDK8。我什至看不到你在哪里进行替换。如果需要帮助,请发布正确的minimal reproducible example
  • 我已经更新了一个工作示例,只需在 eclipse 上运行它
  • @Shaz,toString() 的要点是返回整个对象的表示,而不仅仅是对象的一行。因此,您的 main() 方法中唯一需要的代码是 System.out.println( nums ); 然后使用我建议的代码替换不需要的字符。如果您希望矩阵的每一行打印在不同的行上,那么您需要自定义 toString() 方法来执行此操作。如果你想使用一个迭代器,那么它应该是 toString() 的一部分,而不是 main() 方法。
猜你喜欢
  • 2015-10-21
  • 2014-05-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-13
  • 2023-03-22
  • 2017-08-07
相关资源
最近更新 更多