1 2 Previous Next 17 Replies Latest reply on Oct 3, 2007 3:56 PM by 3004

    how to print a ArrayList?

    807600
      Hi, I am a newbie to Java, would someone pls help me:
      I want to print a ArrayList expQueue, I used ListIterator as following:

           ListIterator iter = expQueue.listIterator();
           while (iter.hasNext()) {
           System.out.print(iter.next()+",");
           }
           System.out.println();

      the expected result should be: (id1, distance1), (id2,distance2), ...,
      but the result likes: query.queuep@1a52fdf, query.queuep@120d62b, query.queuep@1362012.

      what does it mean, and how can I get the expected results?

      thanks in advance.
        • 1. Re: how to print a ArrayList?
          807600
          these are the expected results. for each element, the toString method is called. the output is that of Object.toString. if you want something else, you should override toString in the class of the elements.
          • 2. Re: how to print a ArrayList?
            807600
            Use this           
            Map.Entry pairs = (Map.Entry)Iterator.next();
                                     
            String Key = (String) pairs .getKey();
            String value= (String) pairs .getValue();
            • 3. Re: how to print a ArrayList?
              807600
              AmitChalwade123456 wrote:
              Use this           
              c'mon amit. read the op. what makes you think there's a map's entrySet involved?
              • 4. Re: how to print a ArrayList?
                807600
                OnBringer wrote:
                AmitChalwade123456 wrote:
                Use this           
                c'mon amit. read the op. what makes you think there's a map's entrySet involved?
                Sorry my mistake
                use simple for loop
                and ArrayList.get (index) method is sufficient
                • 5. Re: how to print a ArrayList?
                  807600
                  AmitChalwade123456 wrote:
                  OnBringer wrote:
                  AmitChalwade123456 wrote:
                  Use this           
                  c'mon amit. read the op. what makes you think there's a map's entrySet involved?
                  Sorry my mistake
                  use simple for loop
                  and ArrayList.get (index) method is sufficient
                  not even necessary; ArrayList's toString is overridden to do the looping. all that's needed is for the element type to provide a toString.
                  • 6. Re: how to print a ArrayList?
                    807600
                    for ( int i= 0; i <ArrayList.size();i++){
                                        String value = ArrayList.get(i);
                                   }
                    • 7. Re: how to print a ArrayList?
                      807600
                      not even necessary; ArrayList's toString is overridden to do the looping. all that's needed is for the element type to provide a toString.
                      Can u explain this with example ? Please
                      • 8. Re: how to print a ArrayList?
                        807600
                        thanks giving to all of you two. I get it.
                        • 9. Re: how to print a ArrayList?
                          3004
                          AmitChalwade123456 wrote:
                          for ( int i= 0; i <ArrayList.size();i++){
                                              String value = ArrayList.get(i);
                                         }
                          No, don't do that. It's wrong for a couple of reasons.
                          • 10. Re: how to print a ArrayList?
                            807600
                            >
                            No, don't do that. It's wrong for a couple of reasons.
                            Please tell me those reasons
                            • 11. Re: how to print a ArrayList?
                              807600
                              AmitChalwade123456 wrote:
                              No, don't do that. It's wrong for a couple of reasons.
                              Please tell me those reasons
                              please use your compiler, run it and watch what happens to find out
                              • 12. Re: how to print a ArrayList?
                                3004
                                AmitChalwade123456 wrote:
                                No, don't do that. It's wrong for a couple of reasons.
                                Please tell me those reasons
                                1) It probably won't compile, and if it does, it's because you've got a horribly named variable.

                                2) You should use an iterator or enhanced for loop, not get(i)

                                3) It's clear he's not storing Strings in his list, so String value = is wrong.

                                4) He wants to print. It doesn't print anything.

                                5) It's basically the same thing he's already doing, only worse, for the above reasons. Your post contains no information.

                                6) If you want to print the contents of an ArrayList, and you're not picky about them each being on their own line, System.out.println(theList) will work.

                                7) His printing problem has NOTHING to do with ArrayList and everything to do with the objects he's printing not overrideing toString.
                                • 13. Re: how to print a ArrayList?
                                  3004
                                  I wrote:
                                  >
                                  5) It's basically the same thing he's already doing, only worse, for the above reasons. Your post contains no information.
                                  Seriously, what is it that you think you're doing differently than him that would make it work?
                                  • 14. Re: how to print a ArrayList?
                                    807600
                                    jverd wrote:
                                    AmitChalwade123456 wrote:
                                    No, don't do that. It's wrong for a couple of reasons.
                                    Please tell me those reasons
                                    1) It probably won't compile, and if it does, it's because you've got a horribly named variable.

                                    2) You should use an iterator or enhanced for loop, not get(i)

                                    3) It's clear he's not storing Strings in his list, so String value = is wrong.

                                    4) He wants to print. It doesn't print anything.

                                    5) It's basically the same thing he's already doing, only worse, for the above reasons. Your post contains no information.

                                    6) If you want to print the contents of an ArrayList, and you're not picky about them each being on their own line, System.out.println(theList) will work.

                                    7) His printing problem has NOTHING to do with ArrayList and everything to do with the objects he's printing not overrideing toString.
                                    Thanks a lot for this brief explanation
                                    but
                                         ArrayList arr = new ArrayList();
                                              arr.add("1");
                                              arr.add("2");
                                              arr.add("3");
                                              arr.add("4");
                                              arr.add("5");
                                              
                                              for ( int cnt =0 ; cnt < arr.size();cnt++){
                                                   System.out.println(arr.get(cnt));
                                              }
                                    what u find wrong in this this one works fine at my side .. ??
                                    1 2 Previous Next