Java: How to Print Array Without Square Brackets and Commas

Print array without brackets and commas

Basically, don't use ArrayList.toString() - build the string up for yourself. For example:

StringBuilder builder = new StringBuilder();
for (String value : publicArray) {
builder.append(value);
}
String text = builder.toString();

(Personally I wouldn't call the variable publicArray when it's not actually an array, by the way.)

Java: how to print array without square brackets and commas

    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.

Output ArrayList to String without [,] (brackets) appearing

You could try to replace the '[' and ']' with empty space

String list = Arrays.toString(customers.toArray()).replace("[", "").replace("]", "");

print array of objects without brackets

You can try this

String output ;
for(String s: books[0]){
output = output +s+ ",";
}
output = output.substring(0, output.length()-1);
System.out.print(output);


Related Topics



Leave a reply



Submit