Search code examples
javaarraysstack

Java: how to print array without square brackets and commas


This is my code that I used for my program. I’m having difficulties in displaying it the way I want which is without the brackets and commas. I want it to be like pyramids:

    X                                              #      
   XXX                                            ###     
  XXXXX                                          #####    
 XXXXXXX                                        #######   
XXXXXXXXX                                      #########  

My code gives square brackets and commas that I don’t want. I get:

[    X    ,    XXX   ,   XXXXX  ,  XXXXXXX , XXXXXXXXX]  
[]  
[]  
[]  
[]  
[    #    ,    ###   ,   #####  ,  ####### , #########]  

My code:

Stack stackA = new Stack();   
stackA.push("    X    ");
stackA.push("   XXX   ");
stackA.push("  XXXXX  ");
stackA.push(" XXXXXXX ");
stackA.push("XXXXXXXXX");

Stack stackB = new Stack();
Stack stackC = new Stack();
Stack stackD = new Stack();
Stack stackE = new Stack();

Stack stackF = new Stack();
stackF.push("    #    ");
stackF.push("   ###   ");
stackF.push("  #####  ");
stackF.push(" ####### ");
stackF.push("#########");

Stack[] combine = new Stack[6];
combine[0] = stackA;
combine[1] = stackB;
combine[2] = stackC;
combine[3] = stackD;
combine[4] = stackE;
combine[5] = stackF;

for (int i = 0; i < combine.length; i++)
{
    System.out.print(combine[i] + "  ");
    System.out.println();
}

Solution

  •     for (int line = 0; line <= 5; line++)
        {
            for (int i = 0; i < combine.length; i++) {
                Stack st = combine[i];
                if (st.size() > line) {
                    System.out.print(st.get(line) + "  ");
                } else {
                    System.out.print("           ");
                }
            }
            System.out.println();
        }
    

    This prints

        X                                              #      
       XXX                                            ###     
      XXXXX                                          #####    
     XXXXXXX                                        #######   
    XXXXXXXXX                                      #########  
    

    Challenges include:

    • You can print only lines to System.out, so printing one pyramid at a time will not give you the output you wanted with the pyramids side by side. Instead on each line we need to print one element from each stack that is high enough to have an element on that line. For stacks that are not high enough I print a blank string to make sure the following stacks line up correctly.
    • I am assuming that no pyramid is higher than 5 and that all pyramid elements are exactly 9 chars wide. The code could be refined to take other sizes into account if needed.

    For better use of the Java library classes you may consider the following. It’s not what you asked, and please ignore if you don’t want to use it.

    • Use generics. For example Stack<String> stackA = new Stack<>();. This will allow you to handle the elements you get from the stack as String objects rather than just Objects.
    • Since generic classes don’t always work well inside arrays, you may use a List instead of your array, for example List<Stack<String>> combine = new ArrayList<>(); (optionally give a suggested capacity: new ArrayList<>(6)).
    • The Stack class is considered legacy. The docs say that you should prefer the Deque interface. I recommend the ArrayDeque class, it implements the interface. Like:

      Deque<String> stackA = new ArrayDeque<>(5);
      
      List<Deque<String>> combine = new ArrayList<>(6);
      combine.add(stackA);
      

      To use a Deque as a stack you use its addFirst method for push and its removeFirst for pop. It’s explained in the documentation.

    • You may use the enhanced for loop, for example for (Deque<String> pyramid : combine).

    PS If you wanted your stacks to grow from the bottom, you should probably push the widest element first and print the lines in the opposite order. You’ll find out soon enough.