Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从对象的LinkedList和所有这些对象的LinkedList中删除对象?_Java_Oop_Linked List - Fatal编程技术网

Java 如何从对象的LinkedList和所有这些对象的LinkedList中删除对象?

Java 如何从对象的LinkedList和所有这些对象的LinkedList中删除对象?,java,oop,linked-list,Java,Oop,Linked List,我一直在尝试编写一个方法,不仅可以从LinkedList(allUsers)中删除object(User),还可以从所有用户拥有的各个LinkedList中删除。谁能向我解释一下为什么这是错误的?我已经包括了有问题的方法和用户类 public void removeUser(User u){ //curr equal to front of users list Node curr=allUsers.getFront(); //loop to remove friend

我一直在尝试编写一个方法,不仅可以从LinkedList(allUsers)中删除object(User),还可以从所有用户拥有的各个LinkedList中删除。谁能向我解释一下为什么这是错误的?我已经包括了有问题的方法和用户类

public void removeUser(User u){
    //curr equal to front of users list
    Node curr=allUsers.getFront();
    //loop to remove friend from FriendsList
    while(!curr.getNext().getData().equals(u)||curr.getNext()!=null){
        curr=curr.getNext();
    }
    if(curr.getNext()!=null)
        allUsers.remove(allUsers.indexOf(curr.getNext().getData()));
    //loop to run through allUsers
    curr=allUsers.getFront();
    while(curr!=null){  
        if(curr.getData().getFriends().size()!=0){
            //run through friends LinkedLists of each user
            Node curr2=curr.getData().getFriends().getFront();
            while(curr2!=null){
                if(curr2.getData().equals(u))
                    curr2.getData().removeFriend(u);
                curr2=curr2.getNext();  
            }
        }
        curr=curr.getNext();
    }
}
用户类别:

public class User{ 
    private String name;
    private String location;
    private int birthYear;
    private LinkedList friends;
    public User(String n, String l, int b){
        name=n;
        location=l;
        birthYear=b;
        friends=new LinkedList();
    }
    public String getName(){return name;}
    public String getLocation(){return location;}
    public int getBirthYear(){return birthYear;}
    public boolean equals(User u){
        if(this.getName().equals(u.getName())&&this.getLocation().equals(u.getLocation())&&this.getBirthYear()==u.getBirthYear())
            return true;
        else
            return false;
    }
    public LinkedList getFriends(){return friends;}
    public int getNumFriends(){return friends.size();}
    public String toString(){
        return name+" from "+location;
    }
    public void addFriend(User u){friends.addToEnd(u);}
    public void removeFriend(User u){
        if(friends.indexOf(u)!=-1)
            friends.remove(friends.indexOf(u));
    }
}

让LinkedList为您完成以下工作:

   public void removeUser(User u){
      while (allUsers.remove(u)) {
        // this loops continue until there are no more entries of user in allUsers (as defined by the User.equals() method)
      }
      // now remove this user from all the remaining user's friends list
      for (User user : allUsers) {
        while (user.getFriends().remove(u)) {
          // remove all occurances
        }
      }
   }

让LinkedList为您完成以下工作:

   public void removeUser(User u){
      while (allUsers.remove(u)) {
        // this loops continue until there are no more entries of user in allUsers (as defined by the User.equals() method)
      }
      // now remove this user from all the remaining user's friends list
      for (User user : allUsers) {
        while (user.getFriends().remove(u)) {
          // remove all occurances
        }
      }
   }

alluser有什么问题。删除第一部分的(u)
(我不知道你的问题的第二部分问了什么)。你应该设计它,这样你就不必自己遍历列表,让
LinkedList
自己找到正确的对象。
allUsers有什么问题。第一部分删除(u)
(我不确定你的问题的第二部分问了什么)。您应该设计它,这样您就不必自己遍历列表,并让
LinkedList
处理如何找到正确的对象本身。