[英]how to remove specific object in an ArrayList while using a scanner in java
我正在嘗試從我的輸入(掃描儀)中從ArrayList(memberList)中刪除MemberPlayer(對象)
我曾嘗試環顧Google和Stack,但似乎找不到使用掃描儀的任何東西。
public void removeMember(){
System.out.println("Which MemberPlayer are you looking for?:");
System.out.print("Input first name: ");
String fName = input.nextLine().toUpperCase();
System.out.print("Input last name: ");
String lName = input.nextLine().toUpperCase();
for (MemberPlayer m: memberlist){
if(m.getFirstName().contains(fName) && m.getLastName().contains(lName)) {
System.out.println();
System.out.println("This MemberPlayer exist:");
System.out.println(fName + " " + lName);
System.out.print("Do you want to remove this MemberPlayer? [yes/no]");
input.nextLine().toUpperCase();
if (input.equals("Yes")) {
memberlist.remove(); //I can't figure out how to write this line?
}else{
break;
}
}else {
System.out.println();
System.out.println("This MemberPlayer doesn't exist");
System.out.println();
break;
}
}
}
我從file.txt中讀取了具有以下信息的會員列表:名字,姓氏,年齡和團隊。
ANDERS
ANDERSEN 23 1
BERT BERSEN 16 2
HANS HANSEN 25 1
TIM TIMSEN 20 2
MORTEN MORTENSEN 34 1
您需要使用docs中的List#remove() :
布爾值remove(Object o)
如果存在指定元素,則從列表中刪除該元素的第一次出現(可選操作)。 如果此列表不包含該元素,則它保持不變。 更正式地講,刪除索引i最低的元素,使得(o == null?get(i)== null:o.equals(get(i)))(如果存在這樣的元素)。 如果此列表包含指定的元素(或者等效地,如果此列表由於調用而更改),則返回true。
另外,您在這里不需要for-loop
。 您的方法可以簡化為更多面向對象的方法:
public void removeMember() {
System.out.println("Which MemberPlayer are you looking for?:");
System.out.print("Input first name: ");
String fName = input.nextLine().toUpperCase();
System.out.print("Input last name: ");
String lName = input.nextLine().toUpperCase();
// create an object with input received
MemberPlayer m = new MemberPlayer(fName, lName);
// use contains of List
if (memberlist.contains(m)) {
memberlist.remove(m);
} else {
System.out.println("This MemberPlayer doesn't exist");
}
}
請確保您覆蓋.equals()
和.hashcode()
的方法MemberPlayer
。
我建議為此使用Iterator
,因為使用List.remove(Object o)
會在迭代時更改對象狀態時引發ConcurrentModificationException
。
所以Iterator.remove()
將是一個安全的選擇。 從Java SE 1.8文檔中:
迭代器允許調用者在迭代期間使用定義明確的語義從基礎集合中刪除元素。
因此,使用List.remove()
從List
直接刪除對象將導致不可預測的迭代,並在迭代時引發ConcurrentModificationException
。
如果不進行迭代,則可以使用List.remove(Object o)
從List
刪除該對象。
//Initializes the iterator, checks if next element is present by calling Iterator.hasNext()
for(Iterator<MemberPlayer> itr = memberList.iterator(); itr.hasNext(); ){
m = itr.next(); //The current element of the List
if(m.getFirstName().contains(fName) && m.getLastName().contains(lName)) {
System.out.println();
System.out.println("This MemberPlayer exist:");
System.out.println(fName + " " + lName);
System.out.print("Do you want to remove this MemberPlayer? [yes/no]");
input.nextLine().toUpperCase();
if (input.equals("Yes")) {
itr.remove(); //Removes the current element if the condition is satisfied.
}else{
break;
}
}else {
System.out.println();
System.out.println("This MemberPlayer doesn't exist");
System.out.println();
break;
}
}
請記住,使用for-each
循環迭代Collection<T>
元素時,不能刪除它。 在這種情況下,可能會拋出ConcurrentModificationException
。
你需要明確使用Interator<T>
或ListIterator<T>
根據不同的用例。
ListIterator
還允許插入元素或設置元素。
for (final Iterator<MemberPlayer> iterator = memberList.iterator(); iterator.hasNext();) {
final MemberPlayer m = iterator.next();
if (m.getFirstName().contains(fName) && m.getLastName().contains(lName)) {
...
iterator.remove();
}
}
經過一番嘗試和失敗之后,這才對我有用。
public void removeMember()throws FileNotFoundException {
System.out.println("Which MemberPlayer are you looking to remove?:");
System.out.print("Input first name: ");
String fName1 = input.nextLine().toUpperCase();
System.out.print("Input last name: ");
String lName2 = input.nextLine().toUpperCase();
for (MemberPlayer m : memberlist){
if (m.getFirstName().equals(fName1) & m.getLastName().equals(lName2)) {
System.out.println();
memberlist.remove(m);
System.out.println("You removed: "+m.getFirstName()+" "+m.getLastName());
System.out.println();
saveMember();
break;
} else {
System.out.println();
System.out.println("This MemberPlayer doesn't exist");
System.out.println();
break;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.