我正在向ArrayList添加元素,我将它设计为在用户尝试添加预先存在的元素时显示错误(当它尝试删除不存在的元素时也是如此)。从它显示的内容,它添加了新元素,但仍然表示该元素与ArrayList中已有的元素匹配。我一直在俯瞰什么? ArrayList在FacebookUser.java类中创建和操作。谢谢(如果这是一个愚蠢的错误,我事先道歉。)
DriverClass.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;
}
}
UserAccount.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
答案 0 :(得分:2)
您的循环错误:
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);
}
}
您正在遍历朋友列表,然后在结束时添加它,然后朋友列表变得更大,那么您将与刚刚添加的列表进行比较,并表示它已经存在。< / p>
你想要这个:
boolean alreadyThere = false;
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.");
alreadyThere = true;
break;
}
}
if(!alreadyThere) {
friends.add(newFriend);
}
更简单的说,可能就是这样:
if(friends.contains(newFriend)) {
System.out.println("That person is already in your friends list.");
} else {
friends.add(newFriend);
}
答案 1 :(得分:2)
您在size()
循环中与for
进行比较,即使您还可能在循环中将项目添加到列表中,因此您最终会将项目与自身进行比较最后一次迭代。
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);
}
答案 2 :(得分:1)
你正朝着正确的方向前进,但对最后添加的朋友的迭代就是问题所在。
快速修复可能会在添加后打破循环:
friends.add(newFriend);
break;
但它不是一个合适的解决方案。我们可以在这里使用contains
:
if (friends.contains(newFriend)) {
System.out.println("That person is already in your friends list.");
return;
}
friends.add(newFriend);
答案 3 :(得分:1)
当你将newFriend添加到列表中时,它的大小会增加,因此循环执行一次,实际上将newFriend与其自身进行比较,从而显示消息。
快速解决方法是添加休息时间;但这会让事情变得非常复杂......
对我来说,你可以写:
if (friends.contains(newFriend)) {
System.out.println("That person is already in your friends list.");
} else {
friends.add(newFriend)
}
无需在列表元素上手动循环。
但更简单的解决方案是为FacebookUser实现equals / hashCode(无论如何正确使用包含并找到合适的朋友)并使用一组朋友而不是列表。 Set结构总是确保没有重复,并且如果有很多朋友的话会更快地执行。
private Set<FacebookUser> friends = new HashSet<FacebookUser>();
[...]
if (!friends.add(newFriend)) {
System.out.println("That person is already in your friends list.");
}