Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Java
  4. Printing the contents of a collection

Printing the contents of a collection

Scheduled Pinned Locked Moved Java
questionc++javatutorial
11 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SWDevil
    wrote on last edited by
    #1

    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!

    C D A R 4 Replies Last reply
    0
    • S SWDevil

      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!

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Did you try the toString method ?

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      S 1 Reply Last reply
      0
      • C Cedric Moonen

        Did you try the toString method ?

        Cédric Moonen Software developer
        Charting control [v3.0] OpenGL game tutorial in C++

        S Offline
        S Offline
        SWDevil
        wrote on last edited by
        #3

        toString on what? on arrCol[i]? arrCol[i].toString() gives me a cannot find symbol error.

        C 1 Reply Last reply
        0
        • S SWDevil

          toString on what? on arrCol[i]? arrCol[i].toString() gives me a cannot find symbol error.

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          On the Colection.

          Cédric Moonen Software developer
          Charting control [v3.0] OpenGL game tutorial in C++

          S 1 Reply Last reply
          0
          • C Cedric Moonen

            On the Colection.

            Cédric Moonen Software developer
            Charting control [v3.0] OpenGL game tutorial in C++

            S Offline
            S Offline
            SWDevil
            wrote on last edited by
            #5

            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...

            C 1 Reply Last reply
            0
            • S SWDevil

              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...

              C Offline
              C Offline
              Cedric Moonen
              wrote on last edited by
              #6

              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++

              S 1 Reply Last reply
              0
              • C Cedric Moonen

                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++

                S Offline
                S Offline
                SWDevil
                wrote on last edited by
                #7

                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.

                C 1 Reply Last reply
                0
                • S SWDevil

                  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.

                  C Offline
                  C Offline
                  Cedric Moonen
                  wrote on last edited by
                  #8

                  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++

                  1 Reply Last reply
                  0
                  • S SWDevil

                    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!

                    D Offline
                    D Offline
                    David Skelly
                    wrote on last edited by
                    #9

                    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.

                    1 Reply Last reply
                    0
                    • S SWDevil

                      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!

                      A Offline
                      A Offline
                      Alok Sharma ji
                      wrote on last edited by
                      #10

                      if i am reading right you just need out.print X|

                      1 Reply Last reply
                      0
                      • S SWDevil

                        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!

                        R Offline
                        R Offline
                        Ramaiah Raj
                        wrote on last edited by
                        #11

                        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

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups