?
Solved

toString()

Posted on 2006-03-19
7
Medium Priority
?
885 Views
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()
0
Comment
Question by:no158
7 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 800 total points
ID: 16231563
You just iterate it and append to a StringBuffer
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 800 total points
ID: 16231571
Why don't you *use* a collection and call toString
0
 
LVL 1

Expert Comment

by:winfred_lu
ID: 16232073
dear no158,

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

Winfred Lu
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:no158
ID: 16232102
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
 
LVL 10

Assisted Solution

by:radarsh
radarsh earned 400 total points
ID: 16232804
public String toString() {
    return items.toString();
}

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

________
radarsh
0
 
LVL 92

Accepted Solution

by:
objects earned 800 total points
ID: 16232870
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 16234186
:-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 11 hours left to enroll

839 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