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

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -