-
September 5th, 2010, 08:43 PM
#1
print a List
Hello,
I have a list and I'd like to print it separated byt he comma BUT without the '[' and ']'
Code:
List<String> lists = new ArrayList<String>
system.out.println( lists.toString() );
Is it possible?
thank you.
-
September 5th, 2010, 08:47 PM
#2
Re: print a List
You could substring the String returned by the toString() method and strip off the leading and trailing characters.
Norm
-
September 5th, 2010, 10:16 PM
#3
Re: print a List
Originally Posted by Norm
You could substring the String returned by the toString() method and strip off the leading and trailing characters.
anything simpler?
-
September 6th, 2010, 06:40 AM
#4
Re: print a List
If you want to print the elements stored in the list, just loop over the list and print each element separated by a comma & space.
If not, you'll have to be more specific about exactly what you do want.
I am always doing that which I cannot do, in order that I may learn how to do it...
P. Picasso
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
September 6th, 2010, 06:46 AM
#5
Re: print a List
It depends what you mean by simpler. Norms suggestion is simple and efficient if you know to use the String's substring method.
Alternative ways are use the String's replace method to replace the [ & ] chars with empty strings or write a for loop that iterates over the collection outputting each value and a comma (remembering not to print the comma after the last item).
BTW If you spent less time constantly asking us for simpler and more efficient ways of do things after every response you get you would have more time to learn how to do these things yourself.
-
September 6th, 2010, 07:05 AM
#6
Re: print a List
Originally Posted by keang
It depends what you mean by simpler. Norms suggestion is simple and efficient if you know to use the String's substring method.
Alternative ways are use the String's replace method to replace the [ & ] chars with empty strings or write a for loop that iterates over the collection outputting each value and a comma (remembering not to print the comma after the last item).
BTW If you spent less time constantly asking us for simpler and more efficient ways of do things after every response you get you would have more time to learn how to do these things yourself.
fine, I thought therw was a method that do that...thanks
-
September 6th, 2010, 10:58 AM
#7
Re: print a List
Originally Posted by mickey0
fine, I thought therw was a method that do that...thanks
Obviously, only general purpose methods can be provided, and they are fully documented in the API docs - the library writers can't supply methods to do everything that you want to do. Programming is about writing code to use the methods available to achieve your specific objectives.
If you want code to look simpler, write smaller, simpler methods and hook them together to do what you want.
It goes against the grain of modern education to teach students to program. What fun is there to making plans, acquiring discipline, organizing thoughts, devoting attention to detail, and learning to be self critical...
A. Perlis
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|