-7

如何做一个数组的联系人?那么如何放入字符串并使用“$”作为分隔符?

在主类中制作一个联系人数组,输入联系人。键入后将数据放入字符串中,使用“$”作为分隔符

public class Contato {

    private String name;
    private String address;
    private String numerophone;

    public void setName(String name) {
        this.name = name;
    }

    public void setAddress(String address){
        this.address = address;
    }

    public void setTelefone(String numerophone) {
        this.numerophone = numerophone;
    }

    public String getName() {
        return name;
    }

    public String getAddress() {
        return address;
    }

    public String numerophone() {
        return numerophone;
    }

    @Override
    public String toString() {
        return "name: " + this.name + " address : " + this.address + "  fone : " + this.numerophone;
    }
}
4

3 回答 3

0

创建一个联系人数组:

private static final int MAX_CONTACTS = 100;
Contato[] contacts = new Contato[MAX_CONTACTS];

然后,您可以遍历数组以访问每个联系人:

for (Contato c : contacts) {
    // Do something e.g.
    c.setName("Will");
}
于 2013-06-14T02:35:06.050 回答
0

根据您想要“联系人数组”的字面意思,尝试:

Contact[] foo = {contact1, contact2, contact3...};

或者

List<Contact> bar = new ArrayList<Contact>;
bar.add(contact1);
于 2013-06-14T02:35:59.570 回答
0
public static void main (String ... args) {
    ArrayList<Contanct> contacts = new ArrayList<Contact>();

    Contact c1= new Contact();
    c1.setName("John");
    c1.setAddress("Arthur Street 10");
    c1.setTelephone("123");

    Contact c2= new Contact();
    c2.setName("Peter");
    c2.setAddress("Sam Street 2");
    c2.setTelephone("456");

    contacts.add(c1);
    contacts.add(c2);

    String result= "";
    //Put it to a String
    for (Contact c : contacts) {
        result+=c.toString() + "$";
    }
    result = result.substring(0, result.length() - 1);

    System.out.println(result);
}
于 2013-06-14T02:41:25.783 回答