-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
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 に答える
0

文字通り「連絡先の配列」が必要かどうかに応じて、次を試してください。

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

また

List<Contact> bar = new ArrayList<Contact>;
bar.add(contact1);
于 2013-06-14T02:35:59.570 に答える
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 に答える