CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: print a List

  1. #1
    Join Date
    Jul 2007
    Posts
    273

    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.

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: print a List

    You could substring the String returned by the toString() method and strip off the leading and trailing characters.
    Norm

  3. #3
    Join Date
    Jul 2007
    Posts
    273

    Re: print a List

    Quote Originally Posted by Norm View Post
    You could substring the String returned by the toString() method and strip off the leading and trailing characters.
    anything simpler?

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    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 &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    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.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  6. #6
    Join Date
    Jul 2007
    Posts
    273

    Re: print a List

    Quote Originally Posted by keang View Post
    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

  7. #7
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: print a List

    Quote Originally Posted by mickey0 View Post
    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 &#91;CODE]...your code here...&#91;/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
  •  





Click Here to Expand Forum to Full Width

Featured