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 asString
objects rather than justObject
s. - Since generic classes don’t always work well inside arrays, you may use a
List
instead of your array, for exampleList<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 theDeque
interface. I recommend theArrayDeque
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 itsaddFirst
method for push and itsremoveFirst
for pop. It’s explained in the documentation.- You may use the enhanced
for
loop, for examplefor (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
How to Bypass Google Recaptcha for Testing Using Selenium
Resttemplate: How to Send Url and Query Parameters Together
How to Skip the First Line of a CSV in Java
Setonclicklistener() on a Null Object Reference Error
How to Go About Adding an Image into a Java Project With Eclipse
Java.Sql.Sqlexception: Access Denied for User 'Root'@'Localhost' (Using Password: Yes)
How to Get Values of All Elements from Xml String in Java
Tomcat Is Running But 8080 Port Is Not Responding
How to Pass External Jar File to Spring Boot Project
Launch New Activity from Fragment in Android Studio
Dynamically Change Application.Properties Values in Spring Boot
Cannot Construct Instance of 'Java.Time.Localdate' - Spring Boot, Elasticseach, Jackson
Spring Boot Search Data Between Two Dates
Shifting Characters Within a String
Java.Lang.Noclassdeffounderror: Org/Springframework/Core/Env/Configurableenvironment
Regex to Match Substring After Nth Occurence of Pipe Character
How to Configure Hikaricp in My Spring Boot App in My Application.Properties Files