定義済みのハッシュテーブルにアイテムを追加する場合、通常は単純です。しかし、私がaddNewCustomer()
のようなメソッドを定義し、そのメソッド内でcustomerHashtable.put(...);
関数を使用しようとすると、機能しません。既存のハッシュテーブルで動作するメソッドを定義し、新しいオブジェクト(この場合は顧客)を追加できるようにしてください。ここでJavaで既存のハッシュテーブルに新しい項目を入力するメソッドを定義する方法は?
は、以下のコードは次のとおりです。
public static void main(String[] args) {
Hashtable<Integer, Customer> customerHashtable = new Hashtable<Integer, Customer>();
customerHashtable.put (1, new Customer("david", "+13035003433", new Address("AR", "77555")));
Customer customer = new Customer("mark", "13035003433", new Address("AR", "77200"));
public void addNewCustomers(int key, Customer customer) {
customerHashtable.put(key, customer);
System.out.println(customerHashtable.get(key).toString());
}
}
}
関数に 'customerHashtable'を渡すか、クラスフィールドにする必要があります。 –
それはうまくいかないという意味ですか?より具体的になりますか? –
@ElliottFrisch私はプログラミングに慣れていないのです。もう少し説明していただけますか?私はまた、上記で達成したいコードをコピーしました。 –