gpt4 book ai didi

java - 为什么我的程序在我尝试添加 ArrayList 元素时说它存在,即使它是新元素?

转载 作者:行者123 更新时间:2023-11-29 06:50:55 25 4
gpt4 key购买 nike

我正在向 ArrayList 添加元素,我将其设计为在用户尝试添加预先存在的元素时显示错误(与尝试删除不存在的元素时相同)。从它显示的内容来看,它添加了新元素,但仍然说该元素与 ArrayList 中已有的元素相匹配。我忽略了什么? ArrayList 在 FacebookUser.java 类中创建和操作。谢谢(如果这是一个愚蠢的错误,我提前道歉)。

驱动类.java

public class DriverClass {

public static void main(String[] args) {

FacebookUser fu0 = new FacebookUser("Samuel", "password1");
FacebookUser fu1 = new FacebookUser("Michael", "password2");
FacebookUser fu2 = new FacebookUser("Amy", "password3");
FacebookUser fu3 = new FacebookUser("Eugene", "password4");
fu0.setPasswordHint("p1");
fu3.setPasswordHint("p4");

fu0.friend(fu1);
fu0.friend(fu2);
fu0.friend(fu3);
fu0.friend(fu3);

System.out.println(fu0.getFriends());

fu0.defriend(fu1);
fu0.defriend(fu1);

System.out.println(fu0.getFriends());

fu0.getPasswordHelp();
fu3.getPasswordHelp();
}

}

FacebookUser.java

import java.util.ArrayList;

public class FacebookUser extends UserAccount {

private String passwordHint;
private ArrayList<FacebookUser> friends = new ArrayList<FacebookUser>();

public FacebookUser(String username, String password) {
super(username, password);
friends = new ArrayList<FacebookUser>();
}

@Override
public void getPasswordHelp() {
System.out.println("Password Hint: " + passwordHint);

}

void setPasswordHint(String hint) {
passwordHint = hint;
}

void friend(FacebookUser newFriend) {
System.out.println(friends.size());
if (friends.size() == 0) {
friends.add(newFriend);
} else {
for (int i = 0; i < friends.size(); i++) {
if (friends.get(i).equals(newFriend)) {
System.out.println("That person is already in your friends list.");
break;
} else if (!friends.get(i).equals(newFriend) && i == friends.size() - 1) {
friends.add(newFriend);
}
}
}
}

void defriend(FacebookUser formerFriend) {
if (friends.size() == 0) {
System.out.println("That person is not in your friends list.");
} else {
for (int i = 0; i < friends.size(); i++) {
if (friends.get(i).equals(formerFriend)) {
friends.remove(i);
break;
} else if (!friends.get(i).equals(formerFriend) && i == friends.size() - 1) {
System.out.println("That person is not in your friends list.");
}

}
}

}

ArrayList<FacebookUser> getFriends() {
ArrayList<FacebookUser> friendsCopy = new ArrayList<FacebookUser>();
for (int i = 0; i < friends.size(); i++) {
friendsCopy.add(friends.get(i));
}
return friendsCopy;
}

}

用户帐户.java

public abstract class UserAccount {

private String username;
private String password;
private boolean active;

public UserAccount(String username, String password) {
this.username = username;
this.password = password;
active = true;
}

public boolean checkPassword(String password) {
if (password.equals(this.password)) {
return true;
} else {
return false;
}
}

public void deactivateAccount() {
active = false;
}

public String toString() {
return username;
}

public boolean checkActive() {
if (active == true) {
return true;
} else {
return false;
}
}

public abstract void getPasswordHelp();

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((username == null) ? 0 : username.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
UserAccount other = (UserAccount) obj;
if (username == null) {
if (other.username != null)
return false;
} else if (!username.equals(other.username))
return false;
return true;
}

}

执行:

0
1
That person is already in your friends list.
2
That person is already in your friends list.
3
That person is already in your friends list.
[Michael, Amy, Eugene]
That person is not in your friends list.
[Amy, Eugene]
Password Hint: p1
Password Hint: p4

最佳答案

您在 for 循环中与 size() 进行比较,即使您也可能将项目添加到循环中的列表中,所以您最终比较在最后一次迭代中针对自身的项目。

for (int i = 0; i < friends.size(); i++) { // result of size() will change
if (friends.get(i).equals(newFriend)) {
System.out.println("That person is already in your friends list.");
break;
} else if (!friends.get(i).equals(newFriend) && i == friends.size() - 1) {
friends.add(newFriend);
}
}

您可以只提取调用 size() 的结果,这样它就不会在您添加新项目时发生变化。或者您也可以在添加项目后退出循环。

保存大小:

int size = friends.size();
for (int i = 0; i < size; i++) {
if (friends.get(i).equals(newFriend)) {
System.out.println("That person is already in your friends list.");
break;
} else if (!friends.get(i).equals(newFriend) && i == friends.size() - 1) {
friends.add(newFriend);
}
}

或者,可能更好的是,在您决定应该添加该项目后使用 break:

for (int i = 0; i < friends.size(); i++) {
if (friends.get(i).equals(newFriend)) {
System.out.println("That person is already in your friends list.");
break;
} else if (!friends.get(i).equals(newFriend) && i == friends.size() - 1) {
friends.add(newFriend);
break;
}
}

然后,一个更好的解决方案可能是避免循环并改用 contains:

void friend(FacebookUser newFriend) {
System.out.println(friends.size());
if (friends.contains(newFriend)) {
System.out.println("That person is already in your friends list.");
return;
}

friends.add(newFriend);
}

关于java - 为什么我的程序在我尝试添加 ArrayList 元素时说它存在,即使它是新元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48492197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com