在主类中创建一个Contact数组

时间:2013-06-14 02:21:11

标签: java

如何进行一系列联系? 那么如何使用“$”作为分隔符?

在主类中创建一个Contact数组,键入联系人。键入后使用“$”作为分隔符将数据放入String中

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;
    }
}

3 个答案:

答案 0 :(得分: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");
}

答案 1 :(得分:0)

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

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

List<Contact> bar = new ArrayList<Contact>;
bar.add(contact1);

答案 2 :(得分: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);
}