java - Iterating over an Array to find a string value -
java - Iterating over an Array to find a string value -
public void addsampledata(){ userlist.add(new user("n45","sfdsf","sfgfsg"));
i want iterate through array , able identify , remove object based on string value i.e "sfdsf" rather position in arraylist.
you can iterate on array using for
loop (either basic 1 or enhanced loop):
for (user user : userlist) { // whatever want on item in current iterator position }
if want identify object , potentially compare other objects, have override equals
, hashcode
methods. (better on subject on this link)
if considering field mentioned, sfdsf, user
identifier, can utilize instance variable in comparison; e.g:
for (int = 0; < userlist.size(); i++) { if ("sfdsf".equals(user.getyourfield())) { // note getyourfield refers subject field accessor userlist.remove(i); // remove item if matches criteria } }
java
Comments
Post a Comment