Printing the contents of a collection
-
Hi, I have a web application which has a simple java class. In my java class I have a function that returns Collection. For example: Collection coll = funcA(); I want to somehow print the contents of the coll collection to the screen - this is for debugging purposes. How can I do this? I tried throwing an exception - RuntimeException - but it only accepts a string. I tried converting the collection to a string, but it doesn't work: Collection col = funcA(); String str1 = ""; Object[] arrCol = col.toArray(); for (int i = 0; i < col.size(); i++) { str1 = str1 + arrCol[i]+ " "; } Is there another way I can view during runtime the contents of the collection? By the way, I am very new to java - i am a C++ programmer... Thanks!
-
Hi, I have a web application which has a simple java class. In my java class I have a function that returns Collection. For example: Collection coll = funcA(); I want to somehow print the contents of the coll collection to the screen - this is for debugging purposes. How can I do this? I tried throwing an exception - RuntimeException - but it only accepts a string. I tried converting the collection to a string, but it doesn't work: Collection col = funcA(); String str1 = ""; Object[] arrCol = col.toArray(); for (int i = 0; i < col.size(); i++) { str1 = str1 + arrCol[i]+ " "; } Is there another way I can view during runtime the contents of the collection? By the way, I am very new to java - i am a C++ programmer... Thanks!
Did you try the
toString
method ?Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
Did you try the
toString
method ?Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
On the Colection.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
On the Colection.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
Do you mean: Collection col = funcA(); String sCol = col.toString(); If so it doesn't work... If it's not what you meant, I would appreciate it if you could write some code since I am very new to Java...
SWDevil wrote:
If so it doesn't work...
What do you mean by it doesn't work ?
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
SWDevil wrote:
If so it doesn't work...
What do you mean by it doesn't work ?
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
I mean that it does not print out the contents of the array. it prints out a number of the class or something like that: 116c99d.
You have to override the toString method for your own objects, so that they can be printed out.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
Hi, I have a web application which has a simple java class. In my java class I have a function that returns Collection. For example: Collection coll = funcA(); I want to somehow print the contents of the coll collection to the screen - this is for debugging purposes. How can I do this? I tried throwing an exception - RuntimeException - but it only accepts a string. I tried converting the collection to a string, but it doesn't work: Collection col = funcA(); String str1 = ""; Object[] arrCol = col.toArray(); for (int i = 0; i < col.size(); i++) { str1 = str1 + arrCol[i]+ " "; } Is there another way I can view during runtime the contents of the collection? By the way, I am very new to java - i am a C++ programmer... Thanks!
It's a bit difficult to answer this without knowing what type of Collection you have. Collection is just an interface that can be implemented by any class which can act as a collection of other objects. If it is a List then you can iterate over the contents of the list and print each in turn (using the enhanced for loop syntax is easiest). The same is true for a Set. If it is a Map then the contents of the collection will have two parts: a key and a value, so how you print it will depend on what you want to see (keys, values, or both). There are other more obscure types of Collection like BeanContextServices or JobStateReasons and I have no idea what they contain so you would need to read the documentation on those. Some implementations of Collection have an overridden toString() method that lets you see the contents, but not all do. The simplest thing to do is to call iterator() on the collection and then just loop over that and print whatever it contains. That may be more or less useful depending on what type of collection you have and what is inside it.
-
Hi, I have a web application which has a simple java class. In my java class I have a function that returns Collection. For example: Collection coll = funcA(); I want to somehow print the contents of the coll collection to the screen - this is for debugging purposes. How can I do this? I tried throwing an exception - RuntimeException - but it only accepts a string. I tried converting the collection to a string, but it doesn't work: Collection col = funcA(); String str1 = ""; Object[] arrCol = col.toArray(); for (int i = 0; i < col.size(); i++) { str1 = str1 + arrCol[i]+ " "; } Is there another way I can view during runtime the contents of the collection? By the way, I am very new to java - i am a C++ programmer... Thanks!
if i am reading right you just need out.print X|
-
Hi, I have a web application which has a simple java class. In my java class I have a function that returns Collection. For example: Collection coll = funcA(); I want to somehow print the contents of the coll collection to the screen - this is for debugging purposes. How can I do this? I tried throwing an exception - RuntimeException - but it only accepts a string. I tried converting the collection to a string, but it doesn't work: Collection col = funcA(); String str1 = ""; Object[] arrCol = col.toArray(); for (int i = 0; i < col.size(); i++) { str1 = str1 + arrCol[i]+ " "; } Is there another way I can view during runtime the contents of the collection? By the way, I am very new to java - i am a C++ programmer... Thanks!
Collection c= funcA(); ArrayList al = new ArrayList(c); Sttring a[] = (String[])al.toArray(new String[al.size]); System.out.println("The values in Colections are :"); for (int i=0;i