如何在java中使用扫描程序时删除ArrayList中的特定对象
我正试图通过输入(扫描仪)从ArrayList(成员列表)中删除MemberPlayer(对象) 我试着在google和Stack中搜索,但似乎找不到任何使用扫描仪的东西如何在java中使用扫描程序时删除ArrayList中的特定对象,java,object,arraylist,java.util.scanner,Java,Object,Arraylist,Java.util.scanner,我正试图通过输入(扫描仪)从ArrayList(成员列表)中删除MemberPlayer(对象) 我试着在google和Stack中搜索,但似乎找不到任何使用扫描仪的东西 public void removeMember(){ System.out.println("Which MemberPlayer are you looking for?:"); System.out.print("Input first name: "); String fName = i
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;
}
}
}
我从一个文件.txt中读取我的成员列表,其中包含以下信息:名字、姓氏、年龄和团队
ANDERS
ANDERSEN 23 1
BERT BERSEN 16 2
HANS HANSEN 25 1
TIM TIMSEN 20 2
MORTEN MORTENSEN 34 1
您需要从文档中使用:
布尔删除(对象o)
删除指定的第一个匹配项
元素(如果存在)(可选操作)。如果这
列表不包含该元素,它是不变的。更正式地说,
移除索引i最低的元素,以便(o==null?
get(i)==null:o.equals(get(i))(如果存在这样的元素)。退换商品
如果此列表包含指定的元素,则为true(如果
此列表因调用而更改)
此外,这里不需要循环的
。您的方法可以简化为更加面向对象的方法:
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");
}
}
确保覆盖MemberPlayer
中需要使用的.equals()
和.hashcode()
方法,从以下文档:
布尔删除(对象o)
删除指定的第一个匹配项
元素(如果存在)(可选操作)。如果这
列表不包含该元素,它是不变的。更正式地说,
移除索引i最低的元素,以便(o==null?
get(i)==null:o.equals(get(i))(如果存在这样的元素)。退换商品
如果此列表包含指定的元素,则为true(如果
此列表因调用而更改)
此外,这里不需要循环的。您的方法可以简化为更加面向对象的方法:
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");
}
}
确保覆盖MemberPlayer
中的.equals()
和.hashcode()
方法。我建议为此使用迭代器,就像使用列表一样。删除(对象o)
可以在迭代时更改对象的状态时抛出ConcurrentModificationException
因此,这将是一个安全的赌注。从Java SE 1.8文档中:
迭代器允许调用方从基础结构中删除元素
使用定义良好的语义在迭代过程中收集
因此,使用List.remove()
直接从列表中删除对象将导致不可预测的迭代,并在迭代时抛出ConcurrentModificationException
如果不进行迭代,则可以使用列表。删除(对象o)
从列表中删除对象
//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;
}
}
//初始化迭代器,通过调用iterator.hasNext()检查下一个元素是否存在
for(迭代器itr=memberList.Iterator();itr.hasNext();){
m=itr.next();//列表的当前元素
if(m.getFirstName().contains(fName)和&m.getLastName().contains(lName)){
System.out.println();
System.out.println(“此MemberPlayer存在:”);
System.out.println(fName+“”+lName);
System.out.print(“是否要删除此MemberPlayer?[是/否]”);
input.nextLine().toUpperCase();
如果(输入等于(“是”)){
itr.remove();//如果满足条件,则删除当前元素。
}否则{
打破
}
}否则{
System.out.println();
System.out.println(“此MemberPlayer不存在”);
System.out.println();
打破
}
}
我建议使用迭代器,就像使用列表一样。删除(对象o)
可以在迭代时更改对象的状态时抛出ConcurrentModificationException
因此,这将是一个安全的赌注。从Java SE 1.8文档中:
迭代器允许调用方从基础结构中删除元素
使用定义良好的语义在迭代过程中收集
因此,使用List.remove()
直接从列表中删除对象将导致不可预测的迭代,并在迭代时抛出ConcurrentModificationException
如果不进行迭代,则可以使用列表。删除(对象o)
从列表中删除对象
//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;
}
}
//初始化迭代器,通过调用iterator.hasNext()检查下一个元素是否存在
for(迭代器itr=memberList.Iterator();itr.hasNext();){
m=itr.next();//列表的当前元素
if(m.getFirstName().contains(fName)和&m.getLastName().contains(lName)){
System.out.println();
System.out.println(“此MemberPlayer存在:”);
System.out.println(fName+“”+lName);
System.out.print(“是否要删除此MemberPlayer?[是/否]”);
input.nextLine().toUpperCase();
如果(输入等于(“是”)){
itr.remove();//如果满足条件,则删除当前元素。
}否则{
打破
}
}否则{
System.out.println();
System.out.println(“此MemberPlayer不存在”);
System.out.println();
打破
}
}
请记住,在对每个循环使用进行迭代时,不能删除集合
元素。在这种情况下,可能会抛出一个ConcurrentModificationException
您需要显式使用Interator
或列表迭代器
,具体取决于用例。
列表迭代器还允许插入元素或设置元素
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();
}
}
for(final Iterator Iterator=memberList.Iterator();Iterator.hasNext();){
final MemberPlayer m=iterator.next();
如果(m.getFirstName())包含(fNa