0
我有两个JAVA类用户和用户。用户是将在链接列表中列出用户类实例的主要类。用户应该能够添加和删除用户。我的系数不是在删除。Java链接列表查找框架
import java.util.*;
public class Users {
// Main method
public static void main(String[] args) {
new Users();
}
//attributes
private LinkedList<User> users = new LinkedList<User>();
//Constructors
public Users(){
add();
add();
}
//Methods
//adds a user to the list
private void add(){
users.add(new User());
}
//deletes a user from the list
private void delete(){
User user = user(readName());
if (user != null)
users.remove(user);
else
System.out.println(" No such user");
}
//returns the user if the user exists in the list
private User user(String name){
for (User user: users)
if (user.matches(name)){
return user;
}
return null;
}
private String readName(){
System.out.print(" Names: ");
return In.nextLine();
}
}
User class
public class User {
//Attributes
private String name;
private Users users;
//Constructors
public User(){
this.name = readName();
}
//Methods
//checks if the parameter is equal to the name field
public boolean matches(String name){
return this.name == name;
}
public void add(){
System.out.print(" " + name);
}
public void delete(){
}
public String readName(){
System.out.print(" Name: ");
return In.nextLine();
}
}
在用户类的user(String s)
方法没有递送元件,即使它被添加到列表中。 请一些建议