javaarraysstacktowers-of-hanoi

printing out stack without []


When I print the stack I want it without [] braces. What should I change in my code or how can I do it so the output looks like the one that I show below?

here is my output now

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

I want it to be:

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

here is my code

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]);
        }
    }

Solution

  • Use Arrays.toString():

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