java - Getting the value from custom List -


i have list name declared public , person class have setter , getters methods.in service class has executed method , has list of values.

now want first name list.

so try below...

iterator<person> = name.iterator();  while(it.hasnext()) {      name = (person)it.next();     string firsname=it.next();  } 

please correct me whats wrong here.

no, code not going work, because second cast going fail: it.next() person, not string.

here how can fix code:

list<string> firstnames = new arraylist<string>(); iterator <person> = name.iterator(); // shouldn't persons.iterator() ? while (it.hasnext()) {     person p = it.next(); // no need cast     string firsname = p.getfirstname();     firstnames.add(firstname); } 

alternatively, can this:

list<string> firstnames = new arraylist<string>(); (person p : persons) {     firstnames.add(p.getfirstname()); } 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -