Let arrList be the ArrayList and newValue the new String, then just do:
arrList.set(5, newValue);
This can be found in the java api reference here.
Answer from HaskellElephant on Stack Overflowjava - ArrayList - How to modify a member of an object? - Stack Overflow
java - How do you update an array list - Stack Overflow
arrays - Update specific object items inside Arraylist java - Stack Overflow
How to replace existing value of ArrayList element in Java - Stack Overflow
You can do this:
myList.get(3).setEmail("new email");
Fixed. I was wrong: this only applies on element reassignment. I thought that the returned object wasn't referencing the new one.
It can be done.
Q: Why?
A: The get() method returns an object referencing the original one.
So, if you writemyArrayList.get(15).itsVariable = 7
or
myArrayList.get(15).myMethod("My Value"),
you are actually assigning a value / using a method from the object referenced by the returned one (this means, the change is applied to the original object)
The only thing you can't do is myArrayList.get(15) = myNewElement. To do this you have to use list.set() method.
If you know the position of the element do only the following:
userList.get(index).setUsername("newvalue");
If not, you need to loop all the elements to find the element to update
for (User user : userList) {
if (user.getUserId().equals(searchedId)) {
user.setUsername("newvalue");
break;
}
}
In your case I think it's better using a Map instead of a List:
Map<Integer, User> userMap = new HashMap<Integer, User>();
User user = new User();
user.setUserId(1);
user.setUsername("user1");
userMap.put(user.getUserId(), user);
user = new User();
user.setUserId(2);
user.setUsername("user2");
userMap.put(user.getUserId(), user);
user = new User();
user.setUserId(3);
user.setUsername("user3");
userMap.put(user.getUserId(), user);
In this way, you can search directly for the userId you need:
User userToModify = userMap.remove(idToModify);
userToModify.setUsername("new name");
userToModify.setUserId(54);
userMap.put(user.getUserId(), userToModify);
If you need to find object only by one field (userId, in this case), a Map is far more efficient and easy to use (and to maintain).
Use the set method to replace the old value with a new one.
list.set( 2, "New" );
If you are unaware of the position to replace, use list iterator to find and replace element ListIterator.set(E e)
ListIterator<String> iterator = list.listIterator();
while (iterator.hasNext()) {
String next = iterator.next();
if (next.equals("Two")) {
//Replace element
iterator.set("New");
}
}
This is a continuation of this post.
I created a constructor employeePayReport. In that constructor I made an Array List whose each elements contain different methods of the Employee Class. My Employee Class is parent class and I have 3 child classes for it. I created an object, employeePayReport, in my main method, then I call different methods that would update the elements of ArrayList. I am getting an error while updating them and I am also struggling with how to store them.
Here is the code.
Hi, I'm not at all familiar with ArrayLists in java but I need to use them for this code and really need help. There's my code, I only translated the comments so that's what it does :
-
fill the terrain with ressources (sprout) : adds m (set at 10) sprouts into an arraylist. They are defined through the class Ressource that takes a name (set at the sprout emoji) and an amount set at 0 for the sprout because there's nothing to collect yet. Then they get randomly placed it an 10*10 tab (the println just say whether or not the object was successfully placed) then t.affiche displays the tab
-
make ressources grow (roses) : now that's the one I need help with, from what I researched list.set seemed to be the way to go about changing the elements but their ids are changing and therefore they don't appear in tab. I'm going to put some kind of randomizer on it but if it keeps the id it should just appear in the tab without me having to manually set it's position right ? Could anyone tell me how I could do that ?
ArrayList<Ressource> list= new ArrayList<Ressource> ();
int m = 10; //nb of Ressource
//********************* fill the terrain with ressources (sprout)*******************
for (int i=0; i<m; i++){
list.add(new Ressource(" 🌱 ",0));
if (t.setCase((int)(Math.random()*(9)),(int)(Math.random()*(9)),list.get(i)))
System.out.println("Ajout de " + list.get(i) +" valide !");
else
System.out.println("Ajout incorrect: problème de coordonnées !");
}
System.out.println(list);
t.affiche(5);
System.out.println("Informations sur le terrain:\n"+t);
//********************* make ressources grow (roses)*******************************
for (int i=0; i<m; i++){
list.set(i, new Ressource(" 🌹 ",(int)(Math.random()*(3)));
}
t.affiche(5);You don't have to insert a new item to ArrayList
public void modify(String name) {
for (Item i : item) {
if (i.getName().equalsIgnoreCase(name)) {
System.out.println("New name: ");
String newName = in.nextLine();
i.setName(newName);
}
}
}
It's supposed you have set methods for each field. Then you can update name, price, size this way
You can have an updateName method in the Item class, and then only update the object's name:
item.get(index).updateName(newName);
item.get(index) returns an Item object, on which you apply the updateName method.