toString()

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()
no158Asked:
Who is Participating?
 
objectsCommented:
here you go:

public String toString()
{
     Iterator itr = iterator();
     StringBuffer r = new StringBuffer("[");
     for (int pos = size(); pos > 0; pos--)
     {
         r.append(itr.next());
         if (pos > 1)
           r.append(", ");
     }
     r.append("]");
     return r.toString();
}
0
 
CEHJCommented:
You just iterate it and append to a StringBuffer
0
 
CEHJCommented:
Why don't you *use* a collection and call toString
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
winfred_luCommented:
dear no158,

You may download jdk source code from Sun.
http://www.sun.com/software/communitysource/j2se/java2/download.xml

Winfred Lu
0
 
no158Author Commented:
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.
0
 
radarshCommented:
public String toString() {
    return items.toString();
}

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

________
radarsh
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.