0

I need a solution to change the value of an element in a multidimensional ArrayList in Java. This is my ArrayList: public class Users {

ArrayList<ValidateUser> personer = new ArrayList<ValidateUser>();

public Users() {
  personer.add(new ValidateUser("admin", "asdf123", 0.8, "admin"));
  personer.add(new ValidateUser("jesper", "ukamm19", 2.5, "user"));
  personer.add(new ValidateUser("lars", "lol123", 1.5, "user"));
}

I want to change the double value (0.8) at the user "admin", for example. This would be done from another a class. How to do so?

Thanks in advance! :)

4
  • 2
    This doesn't look multidimensional. Just iterate through the list to find the object. If you're going to do this a lot, consider using a map. Commented Nov 22, 2013 at 15:24
  • As @ZongZhengLi already said, locate the object within the list and change its value. That's all. Commented Nov 22, 2013 at 15:26
  • I'm very new to java, so sorry if i'm incorrect in the term of multidimensional. Commented Nov 22, 2013 at 15:27
  • not probs, just use get method of list and change value of object you get.. :) Commented Nov 22, 2013 at 15:28

2 Answers 2

1

As I've stated in my comment, just iterate through the list to find the object. If you're going to do this a lot, consider using a map.

for (ValidateUser user : personer)
    if (user.getName().equals("admin"))
        user.setNumber(someNumber);
Sign up to request clarification or add additional context in comments.

2 Comments

WOuldn't this throw a ConcurrentModificationException ?
@Adarsh Nope, we're modifying the object, not the list.
1

First, note that this is not a multidimensional array, is just a list that holds elements of ValidateUser class object references. Second, you need to access to the element before updating it. You have several ways to accomplish this:

  1. Implement the equals and hashCode methods in your ValidateUser class, then just retrieve the object from your List:

    ValidateUser adminUser = personer.get(new ValidateUser("admin", "", 0.8, ""));
    adminUser.set...
    

    Note: this looks ridiculous but will work (assuming your equals method only checks by the field that holds this "admin" value.

  2. Navigate through the array and seek for the desired element manually, then update it:

    for (ValidateUser user : personer) {
        if ("admin".equals(user.getXxx()) {
            user.set...
            break; //don't forget this!
        }
    }
    
  3. Use a different data structure like a Map<String, ValidateUser> to store your data and faster retrieval:

    Map<String, ValidateUser> personerMap = new LinkedHashMap<String, ValidateUser>();
    personerMap.add("admin", new ValidateUser("admin", ...);
    //fill the map with other values...
    //if you still want a Collection<ValidateUser> personer variable
    Collection<ValidateUser> personer = personerMap.values();
    
    //now check for the desired element
    ValidateUser admin = personerMap.get("admin");
    if (admin != null) {
        admin.set...
    }
    

By comments, your ValidateUser is an immutable object, so you cannot update its fields using setters (because there aren't). So, the best approach here is to use a ListIterator<ValidateUser> instead (not to confuse with Iterator) and replace the element by your modified object. Here's an example:

//the new immutable ValidateUser that will replace the older one...
//set the parameters as needed
ValidateUser newAdmin = new ValidateUser("admin", ...);
ListIterator<ValidateUser> listIterator = personer.listIterator();
while (listIterator.hasNext()) {
    ValidateUser validateUser = listIterator.next();
    if ("admin".equals(validateUser.getXxx()) {
        listIterator.set(newAdmin);
        break;
    }
}

6 Comments

Hi Luiggi, thanks for a great answer. I'm going with option 2, but is having a problem with .set method as well. I have a double variable called "temp", which should be replacing the content. My code goes as users.personer.set(i, temp); in my for-loop, but it gives me error. What is the problem?
Instead of use personer.set(index, temp), find the ValidateUser validateUser object in your personer list, then modify this validateUser object (by using its set methods).
My ValidateUser does not have set-method, they all get set by the constructor in ValidateUser.
@JesperBaungårdBruunHansen you have to note that in the question then. I'll update my answer accordingly.
yeah, my bad. Im so lost with this code, the help is very appreciated! :)
|

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.