Java链表查找框架

Java链表查找框架,java,linked-list,Java,Linked List,我有两个JAVA类User和Users。Users是主类,它将在链接列表中列出User类的实例。用户应该能够添加和删除用户。我的coe没有执行删除操作 import java.util.*; public class Users { // Main method public static void main(String[] args) { new Users(); } //attributes private LinkedList<User> users = new L

我有两个JAVA类User和Users。Users是主类,它将在链接列表中列出User类的实例。用户应该能够添加和删除用户。我的coe没有执行删除操作

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();
}

}
import java.util.*;
公共类用户{
//主要方法
公共静态void main(字符串[]args){
新用户();
}
//属性
私有LinkedList用户=新建LinkedList();
//建设者
公共用户(){
添加();
添加();
}
//方法
//将用户添加到列表中
私有void add(){
添加(新用户());
}
//从列表中删除用户
私有void delete(){
User=User(readName());
如果(用户!=null)
用户。删除(用户);
其他的
System.out.println(“无此类用户”);
}
//如果列表中存在用户,则返回该用户
私有用户(字符串名称){
for(用户:用户)
if(user.matches(name)){
返回用户;
}
返回null;
}
私有字符串readName(){
系统输出打印(“名称:”);
在.nextLine()中返回;
}
}
用户类
公共类用户{
//属性
私有字符串名称;
私人用户;
//建设者
公共用户(){
this.name=readName();
}
//方法
//检查参数是否等于名称字段
公共布尔匹配(字符串名称){
返回this.name==name;
}
公共无效添加(){
系统输出打印(“+”名称);
}
公共作废删除(){
}
公共字符串readName(){
系统输出打印(“名称:”);
在.nextLine()中返回;
}
}
在Users类中,
user(String s)
方法没有传递元素,即使元素已添加到列表中。
请提供一些建议

您的删除方法需要将要删除的用户对象作为参数传入。当前,您正在声明一个新的用户对象,该对象不包含任何有关要删除的用户的信息,除非您提示输入该用户的名称。您应该将user对象传递到delete方法中,使其看起来像这样

//deletes a user from the list
private void delete(User user) {
    if (user != null) 
        users.remove(user);
    else
        System.out.println("     No such user");
}