简体   繁体   中英

Outputing string values from objects saved in an ArrayList

I have a small problem with printing out strings that are stored in an object. The object is stored in an ArrayList.

I have three clases that I use im my program:

Friend Class:

package one;

public class Friend implements InterfaceFriend {

    private String name;
    private String email;
    private String phone;

    public Friend(String name, String phone, String email) {
        // TODO Auto-generated constructor stub
    }

    @Override
    public String getPhone() {
        return phone;
    }

    @Override
    public String getEmail() {
        return email;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public void setPhone(String phone1) {
        phone1 = phone;
    }

    @Override
    public void setEmail(String email1) {
        email1 = email;
    }

    @Override
    public void setName(String name1) {
        name1 = name;
    }

}

FriendInterface:

package one;

public interface InterfaceFriend {
    String getPhone(); // Returns phone number.

    String getEmail(); // Returns email.

    String getName(); // Returns name.

    void setPhone(String phone); // Sets phone.

    void setEmail(String email); // Sets email.

    void setName(String name); // Sets name.
}

and Test Class:

package one;

import java.util.ArrayList;
import java.util.List;

public class FriendTest {

    static List<Friend> friends;
    static Friend friend;


    public static void main(String args[]) {

        friends = new ArrayList<Friend>();

        friend = new Friend("Jane Doe", "085-5555555", "jane.doe@gmail.com");
        friends.add(friend);

        friend = new Friend("John Doe", "085-1111111", "john.doe@gmail.com");
        friends.add(friend);

        friend = new Friend("Paul Weller", "085-3333333", "paul.weller@gmail.com");
        friends.add(friend);

        System.out.println("Friends added to list:");

        System.out.println(friends.toString());

    }
}

The problem is that when I am running the System.out.println(friends.toString()); from the Test Class i am getting this: Friends added to list: [one.Friend@38f0b51d, one.Friend@4302a01f, one.Friend@615e7597]

Instead the Strings with the values that I want. Any help appreciated.

You'll need to override the toString() method in the Friend class as commented already, but you also need to complete the constructor that you're using.

public Friend(String name, String phone, String email) {
    this.name = name;
    this.phone = phone;
    this.email = email;
}

Moreover, the code in your setters are backwards.

In you friend class you need to override toString()

public class Friend implements InterfaceFriend {
    ...
    ...

    public String toString(){
        return name + " " + email + " " + phone; // or whatever format you want printed
    }
}

you simply override toString method. place this inside of Friend class. problem solved..

public String toString(){
   // return  your Strings..
}

Override to toString() in your class, to what output suits you. The reason you're getting that output is because the default toString() provided in objects is a hash code.

public String toString(){
   return name; //assuming you only want to display the name else edit it

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM