打印出没有 [] 的堆栈

printing out stack without []

当我打印堆栈时,我希望它没有 [] 大括号。 我应该在我的代码中更改什么或如何更改才能使输出看起来像下面显示的那样?

这是我现在的输出

t0   Pillar1: [3, 2, 1]
t0   Pillar2: []
t0   Pillar3: []

我希望它是:

t0 Pillar1: 3 2 1
t0 Pillar2:
t0 Pillar3:

这是我的代码

private int numberOfDisks;
    private int startPillar;
    private int targetPillar;
    private int extraPillar;
    
    private Stack<Integer>[] pillars = new Stack[3];
    private int steps;
    
    public Hanoi(int n, int start, int target)
    {
        numberOfDisks = n;
        startPillar = start-1;
        targetPillar = target;
        extraPillar = 6 - start - target;
        steps = 0;
        
        pillars[0] = new Stack<Integer>();
        pillars[1] = new Stack<Integer>();
        pillars[2] = new Stack<Integer>();

        for(int i = n; i > 0; i--)
        {
            pillars[startPillar].push(i);
        }
        printStep();
        
        
    }
    
    
    private void printStep()
    {
        for(int i=1;i<4;i++)
        {
            System.out.println("t"+steps+"   " + "Pillar" + (i) + ": " + pillars[i-1]);
        }
    }

使用Arrays.toString():

System.out.println(Arrays.toString(pillars.toArray()).replace("[","").replace("]",""));

IMO 最快的方法是先将其放入字符串中

String result = pillars[i-1];
result = result.replace ("[", "").replace("]", "");
System.out.println("t"+steps+"   " + "Pillar" + (i) + ": " + result);