Results 1 to 6 of 6
  1. #1
    anthrax is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Question how do i get elements from java beans from list?

    i have public JavaBean(String name, String phone) {
    this.name = name;
    this.number = phone;}

    i stored multiple rows of data on it bean = JavaBean( result.getString(1),result.getString(2));

    then stored it on ArrayList list.add(bean);
    when i System.out.println(list.get(0));
    it printed package.JavaBean@ae533a
    result.getString(1) is = A and result.getString(2) is = B

    how do i do it?

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    Ah, by default objects when printed are done with the "toString()" method that lives in the java.lang.Object, which is what is generating the package.JavaBean@ae533a looking thing.

    You would need your bean to override the toString() method and have it generate what ever it is you want it to display when it is converted to a String, such as when it is being fed to System.out.println().


    Add this to your bean:
    Java Code:
      public String toString() {
        return " name=" + this.name + " number=" +this.number;
      }
    Or perhaps a better way using string builder.
    Java Code:
      public String toString() {
        StringBuilder b = new StringBuilder();
        b.append(" name=").append(this.name);
        b.append(" number=").append(this.number);
        return b.toString();
      }

  3. #3
    anthrax is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    wow tnx a lot its just what i need :D this info is a great christmas gift for me and to the readers(who needs it) :D

    another question. why is string builder a better way which is the 2nd code? they all work the same as i see their result and both be added in bean

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    The StringBuffer and StringBuilder classes can sometimes result in fewer objects being created. This is discussed in these notes for example.

    Personally I would write:

    Java Code:
    public String toString() {
        return String.format("name=%s number=%s", name, number);
    }

    I find the Formatter syntax simple and expressive.

  5. #5
    coltragon is offline Senior Member
    Join Date
    Dec 2009
    Posts
    104
    Rep Power
    0

    Default

    Quote Originally Posted by anthrax View Post
    i have public JavaBean(String name, String phone) {
    this.name = name;
    this.number = phone;}

    i stored multiple rows of data on it bean = JavaBean( result.getString(1),result.getString(2));

    then stored it on ArrayList list.add(bean);
    when i System.out.println(list.get(0));
    it printed package.JavaBean@ae533a
    result.getString(1) is = A and result.getString(2) is = B

    how do i do it?
    i was just curious, why there he uses doule ")"

  6. #6
    anthrax is offline Member
    Join Date
    Feb 2009
    Posts
    57
    Rep Power
    0

    Default

    System.out.println(list.get(0)) has double ")" bec. System.out.println() has 1 ")" and list.get(0) has another ")" = 2 ")"

Similar Threads

  1. Replies: 27
    Last Post: 08-27-2009, 03:05 PM
  2. Capture elements from an List in a different package
    By blevault in forum New To Java
    Replies: 5
    Last Post: 11-27-2008, 09:14 AM
  3. How to make Beans Lazily-instantiating beans
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-30-2008, 10:10 AM
  4. How to make Beans Lazily-instantiating beans
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-26-2007, 08:41 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
  •