user1143110
user1143110

Reputation: 67

Java: Object ArrayList

I am saving in an arraylist in this way ("name", "surname", "id", "email", "score")

I trying to edit a user in this arraylist by first searching using id and that part i'm ok with it but then I need to keep the same name,surname and email and just change the score.

Can someone help me, thanks.

Upvotes: 1

Views: 499

Answers (2)

Sleiman Jneidi
Sleiman Jneidi

Reputation: 23329

first of all create a class User

  class User{
  String name;
  String Email;
  int Score; 
  // bla bla bla
  }

store your users objects in a HashMap

HashMap<int,User> map=new HashMap<int,User>();
User u1=new User();
map.put(1,u1);

and update the object in the map

Integer x=3;
if(map.containsKey(x)){
User u=map.get(x);
u.score=0;
map.put(x,u);
}

Upvotes: 3

albatross
albatross

Reputation: 455

Why dont you create a new class whch has attributes("name", "surname", "id", "email", "score") and afterwards you can get the index that you want if(myarr[i].ID == 1234) and then you can update that index or get the items and equal them to new Class object then delete that item in the required index and then put that index new object again?

Upvotes: 1

Related Questions