Results 1 to 5 of 5
  1. #1
    tommyyyy is offline Member
    Join Date
    Mar 2009
    Posts
    10
    Rep Power
    0

    Default ArrayList printing

    Hello all, does anybody can tell me how to print object value in the arraylist

    every time i print the arraylist i will get
    MPD@6eb38a, MPD@1cd2e5f, MPD@19f953d,

    b=temp[0];
    m=temp[1];
    r=temp[2];
    ttArray.add(i, new MPD(b,m,r));

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    does the MPD class have a toString method? If not, I'd add one.

  3. #3
    tommyyyy is offline Member
    Join Date
    Mar 2009
    Posts
    10
    Rep Power
    0

    Default

    No, could u tell me how to use toString method

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You give your class a method with the following signature:
    Java Code:
    public String toString()
    {
      // return a String that holds info relevant to your class
    }
    e.g.,
    Java Code:
    package yr2009.m03.c;
    
    public class Main
    {
      public static void main(String[] args)
      {
        Spam spam = new Spam("Spam, Spam, Spam, Spam"); // no toString
        Phlam phlam = new Phlam("Phlam, Phlam, Phlam, Phlam"); // has toString
        
        System.out.println(spam);
        System.out.println(phlam);
      }
    }
    
    class Spam
    {
      String title;
      public Spam(String title)
      {
        this.title = title;
      }
    }
    
    class Phlam
    {
      String title;
      public Phlam(String title)
      {
        this.title = title;
      }
      @Override
      public String toString()
      {
        return "title: " + title;
      }
    }

  5. #5
    tommyyyy is offline Member
    Join Date
    Mar 2009
    Posts
    10
    Rep Power
    0

Similar Threads

  1. Help in Printing
    By kirly in forum Advanced Java
    Replies: 3
    Last Post: 10-03-2011, 04:40 PM
  2. Printing Help...
    By chiragkini in forum AWT / Swing
    Replies: 1
    Last Post: 02-17-2009, 07:07 AM
  3. Help printing specific ArrayList elements
    By CirKuT in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 01:24 AM
  4. Printing Example
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-11-2008, 05:41 PM
  5. Replies: 2
    Last Post: 04-21-2008, 12:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •