1

I have searched a lot for this, and checked the posts that is provided as possible answers, and none seems to give me an answer.

I have this arraylist in which i store online users. I can read from the user list and add to it. Problem is, I cant seem to find out how I remove it.

I have tried

online.remove("MyUsername");

My class and initialiser is like this:

 ArrayList<userOnline> online = new ArrayList<userOnline>();

   class userOnline {
        String userName;
        String data1;
        String data2;
        String data3;
    }

I thought it would find the object row with username and remove the row, or at least the username, but it removed nothing and does not give me any errors.

What can I do to make it work? Or what can I use as an alternative if this is not possible? A pointer to a doc explaining would be more than enough help!

Thanks!

Seemed like the solution was this, but this is not considered good practice

for (int i=0; i <online.size(); i++) {
    if(online.get(i).userName.equals("username")) {
        online.remove(i);
    }
}

After a discussion and a lot of feedback seems like the only right way for java to handle this search and remove is,

  Iterator<userOnline> it = online.iterator();
      while (it.hasNext()) {
          userOnline user = it.next();
          if (currentLogin.equals(user.userName)) {
              it.remove();
          }
     }
12
  • 1
    You need to define equals() for userOnline class so to be able to locate and remove the object. Commented Feb 22, 2017 at 0:26
  • You can either remove using the index of the element or remove using the element's object Commented Feb 22, 2017 at 0:27
  • but not making equals() return true for a String Commented Feb 22, 2017 at 0:27
  • 2
    Or you will need to search the List of a corresponding element and remove it either by the reference of the object or index point Commented Feb 22, 2017 at 0:27
  • Ok, Like i do where i find if a user is online, there i do a for loop to run through and do an equals then print. is that what you mean? Commented Feb 22, 2017 at 0:30

6 Answers 6

5

I couldn't find a dupe or a suitable doc, so here it is:

Use an Iterator:

for (Iterator<userOnline> iterator = online.iterator(); iterator.hasNext();) {
    if (iterator.next().getName().equals("MyUsername")) {
        iterator.remove();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This will remove/search all objects with "MyUsername" in each of the online arrays right? So it is good for doing a search etc?
2

Basically, you can't compare apples and pears (String and userOnline) directly. Yes you could override equals, but it should really match all the properties, not just one.

A simple solution would be to search the List, comparing each objects userName property with the value you want an either return the index or object reference, which you could use to remove it.

Alternatively, you could use an Iterator and remove it as you search...

ArrayList<userOnline> online = new ArrayList<>();
userOnline newUser = new userOnline();
newUser.userName = "MyUsername";
online.add(newUser);
System.out.println(online.size());
Iterator<userOnline> it = online.iterator();
while (it.hasNext()) {
    userOnline user = it.next();
    if ("MyUsername".equals(user.userName)) {
        it.remove();
    }
}
System.out.println(online.size());

There's probably also a really cool "streams" based solution, but small steps ;)

Comments

2

You could create a function that takes in your list of users and finds the first occurence of a given name and removes it when it finds a user with the name given like so

public Array<userOnline> removeUserByName(Array<userOnline> users, String nameToFind)
{
    for(int i = 0; i < users.size(); i++)
    {
        if(users.get(i).userName.equals(nameToFind))
        {
            users.remove(i);
            return users;
        }
    }
    return users;
}

You could also make this function part of the class you store your list of userOnline objects then you wouldn't have to pass the array into the function.

Comments

2

You must search through the userOnline objects contained within your ArrayList and either find the index of the match or a reference to the match. Once you have either of these, you can remove the object from the list using one of the overloaded remove() methods. Remember that by default, the equals method compares references.

The search can be as follows:

private userOnline findUserOnlineWithUsername(String username) {
    Iterator<userOnline> it = online.iterator();
    onlineUser olu = null;

    while(it.hasNext()) {
        olu = it.next();
        if (olu.userName.equals(username)) { return olu;}
    }

    return null;
}

Comments

2

Iterate over the list to find the index of the element you are interested in:

 int idx = -1;
    for (int i = 0; i < online.size(); i++) {
        if(online.get(i).userName.equals("MyUsername"))
        {
            idx = i;
        }
    }

Use this index to remove the relevant element:

if(idx != -1) {
    online.remove(online[idx]);
}

This would only remove the first occurrence. You could put this code into a function and call repeatedly to find all occurrences.

3 Comments

@MadProgrammer Thanks. Several different programming languages were used today.
Yep, tell me about it :P
@sgrg , could you explain the difference between the answer i added in my post and this one?
0

Your code is asking to remove a String from a List of UserOnlines, you need to use the object reference for the remove(Object o) method, or you need to find out the index of the object you wish to remove and use the remove(int index) method. How are you adding your objects to the list? If you're using the list itself as a reference you'll need to create your own method to define what object "MyUserName" is supposed to be.

2 Comments

Ok, I'm creating a new userOnline(); and put the variables to the new user and I add new user to the arraylist.
If you're doing something like the following: UserOnline myUser = new UserOnline() then you can just do list.remove(myUser). However if you're doing list.add(new UserOnline()) as others have mentioned you have to define how you're program finds the object you want. There's been a few suggestions for looking at your name field if that's what you want to use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.