プロパティオブジェクトを含む配列があります。これらのオブジェクトには、リスト番号、文字列のファーストネーム、および整数年があります。
ランダムなリスト番号を入力するときは、配列をチェックしてそのリスト番号が存在するかどうかを確認し、存在する場合はそのプロパティを削除します
リスト番号を取得するために配列オブジェクトにアクセスする方法がわかりません
これが今のところ
public class Property {
//primary fields
private String listingNumber;
}
public Property(String listingNumber) {
this.listingNumber = listingNumber;
}
配列を持つクラス
private Property[] properties;
セールスエージェントと呼ばれています
public SalesAgent(String company) {
this.company = company;
this.properties = new Property[]{};
}
public void removeProperty(String listingNumber) {
if(listingNumber != null){
boolean found = false;
for(int ndx = 0; ndx < this.properties.length && !found; ndx++) {
if(getListingNumber() == listingNumber) {
properties[ndx] = null;
found = true;
}
}
}
Property
リストからを削除する場合は、可変サイズのものが必要です。配列のサイズは固定されているため、を使用することをお勧めしますList
。
それproperties
がであるとすると、List<Property>
次のように書くことができます:
int foundIndex = -1;
for (int i = 0; i < properties.size(); i++) {
if (Objects.equals(properties.get(i).getListingNumber(), listingNumberToFind)) {
foundIndex = i;
break;
}
}
if (foundIndex != -1) {
properties.remove(foundIndex);
}
Property
オブジェクトを含むリストのウォークスルーを開始し、リスト文字列を持つオブジェクトが見つかった場合は、そのインデックスをキャプチャしてウォークを停止します。そのようなインデックスが見つかった場合は、このインデックスを持つオブジェクトをリストから削除します。
これは、Java 8ストリームでも実現できます。
properties = properties.stream()
.filter(p -> !Objects.equals(p.getListingNumber(), listingNumberToFind))
.collect(Collectors.toList());
いくつかのメモ:
removeProperty
Property
クラスに属していません。Sales
それはおそらくそこに存在する必要がありますので、このクラスは、プロパティのリストを保持しています。
常にメソッドとString
sを比較する必要がありequals
ます。参照タイプ(つまりString
)の==
場合、値ではなくオブジェクトIDを比較します。私は、内部で使用する静的メソッドObjects.equals
を使用equals
しました。
このようなリスト番号を頻繁に検索する場合はMap
、リスト番号を対応するProperty
オブジェクトにマッピングするを使用できます。
Map<String, Property> properties = new HashMap<>();
// Put your values into the map
properties.put(«listingNumber», «property»);
...
properties.remove(listingNumber);
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加