java - How find common number from two textview -
i have 2 textview , 2 buttons.
textview tv1,tv2; button yes,no;
now
tv1.settext("1 2 3 4 5"); tv2.settext("3 4 5 6 7");
and
yes.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { //here want show common number (3,4,5) } }); no.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { //here want show number no matched in both textview } });
now question when click on yes show (3,4,5 because common) tv1 , when click on no show (1,2,6,7) tv1.
i can set text if have more numbers.
you may put number array
or list
easy find common number.
public list<integer> findcommonelement(int[] a, int[] b) { list<integer> commonelements = new arraylist<integer>(); for(int = 0; < a.length ;i++) { for(int j = 0; j< b.length ; j++) { if(a[i] == b[j]) { //check if list contains common element if(!commonelements.contains(a[i])) { //add common element list commonelements.add(a[i]); } } } } return commonelements; }
and may check using:
int myarray[] = { 2, 2, 7, 7, 2, 1, 5, 4, 5, 1, 1 }; int myarray2[] = { 2, 3, 4, 7, 10 }; log.d("common", string.valueof(findcommonelement(myarray, myarray2)));
referral: here
Comments
Post a Comment