Posted on 2006-03-19
Last Modified: 2008-03-03
I'm trying to make a toString method that will mimic a Collection toString()

Looking for actual code of the method or code of the Collection toString()
Question by:no158
    LVL 86

    Assisted Solution

    You just iterate it and append to a StringBuffer
    LVL 86

    Assisted Solution

    Why don't you *use* a collection and call toString
    LVL 1

    Expert Comment

    dear no158,

    You may download jdk source code from Sun.

    Winfred Lu

    Author Comment

    I'm working with:
    private LinkedList<AnyType> items;

    public String toString() {
        Iterator<AnyType> str = items.iterator();
        return str;

    I'm having a problem with incompatible types, where did I go wrong.
    LVL 10

    Assisted Solution

    public String toString() {
        return items.toString();

    Should do the trick! This is addition to CEHJ's comment.

    LVL 92

    Accepted Solution

    here you go:

    public String toString()
         Iterator itr = iterator();
         StringBuffer r = new StringBuffer("[");
         for (int pos = size(); pos > 0; pos--)
             if (pos > 1)
               r.append(", ");
         return r.toString();
    LVL 86

    Expert Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    Title # Comments Views Activity
    tenRun challenge 28 67
    Connect to IP Camera using Java 4 58
    wordsFront challenge 8 47
    dividesSelf challange 15 57
    For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
    Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now