简体   繁体   English

从保存在ArrayList中的对象输出字符串值

[英]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. 该对象存储在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: 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()); 问题是当我运行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] 从测试班获得的: 朋友添加到列表:[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. 您已经需要如前所述重写Friend类中的toString()方法,但是还需要完成所使用的构造函数。

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. 而且,setter中的代码是向后的。

In you friend class you need to override toString() 在您的朋友类中,您需要重写toString()

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

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

you simply override toString method. 您只需覆盖toString方法。 place this inside of Friend class. 将此放置在Friend类中。 problem solved.. 问题解决了..

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

Override to toString() in your class, to what output suits you. 将class中的toString()重写为适合您的输出。 The reason you're getting that output is because the default toString() provided in objects is a hash code. 获得该输出的原因是因为对象中提供的默认toString()是哈希码。

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

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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