java - index of in Arraylist<ABC> compare with other Arraylist -
i trying find index of element in arraylist based on value of 1 of properties, giving me -1.
//returnabclinklist method returns abc linked list , cannot use index of on linked list trying convert arraylist list<abc> temp=new arraylist<abc>(somemethod.returnabclinklist()); list<xyz> other=new arraylist<xyz>();
lets abc
has 3 fields (rollnum
, name
, state
) , xyz
has 5 fields, 3 of in common abc
(rollnum
, name
, state
, secondname
, dob
). want iterate through 1 list , find each corresponding element in other list, based on rollnum
values being same. goal fill out other corresponding fields (name
, state
). here's tried:
iterator<abc> itr = abclist.iterator(); while(itr.hasnext()){ abc tempabc=itr.next(); int index = xyzlist.indexof(tempabc.rollnum()); //this comes -1 }
the problem indexof()
returning -1. can implementation?
actual working snippet
import java.util.arraylist; import java.util.iterator; import java.util.list; public class testlists { public static void main(string[] args){ list<xyz> temp=new arraylist<xyz>(); list<abc> other=new arraylist<abc>(); abc ab=new abc(); abc ab1=new abc(); ab.rollnum=111; ab.name="mak"; other.add(ab); ab1.rollnum=222; ab1.name="dak"; other.add(ab1); xyz abd=new xyz(); xyz abd1=new xyz(); abd1.namedb="mak"; abd1.rollnumdb=111; temp.add(abd1); abd.namedb="ponty"; abd.rollnumdb=456; temp.add(abd); iterator<xyz> itr=temp.iterator(); while(itr.hasnext()){ xyz tempxyz=itr.next(); int index=other.indexof(tempxyz.getrollnumdb()); //this comes -1 other.get(index) //get data //add more values tempxyz }); } }
pojo
public class abc { int rollnum; string name; string state; public int getrollnum() { return rollnum; } public void setrollnum(int rollnum) { this.rollnum = rollnum; } public string getname() { return name; } public void setname(string name) { this.name = name; } } public string getstate() { return name; } public void setstate(string name) { this.name = name; } }
note: tried implementing equals in xyz pojo still no luck
the argument indexof
hidden inside xyz
object instance, not of type xyz
. in case have iterate through xyz
list, rollnum
out of it, , compare.
in general i'd recommend having @ map
. save xyz
elements rollnum
key, somemap.get(rollnum)
gives corresponding xyz
element.
Comments
Post a Comment