I have two Arraylists:
ArrayList a1 = new ArrayList();
a1.add("5");
a1.add("10");
a1.add("20");
a1.add("50");
a1.add("100");
a1.add("500");
a1.add("1000");
ArrayList a2 = new ArrayList();
a2.add("50");
a2.add("500");
a2.add("1000");
How can I compare this two arraylists and add into new arraylist(a3) with 1 if a2 exist in a1 and 0 if not exist, so the result will below for arraylist a3?
a3[0] = 0
a3[1] = 0
a3[2] = 0
a3[3] = 1
a3[4] = 0
a3[5] = 1
a3[6] = 1
Thanks in advance
First, I would advice you to use generics. And secondly, for
a2could be aSetinstead. And thirdly you might want to change fromStringtoInteger(since they are all integers).But for your example this is a way to do it:
Full example (with a
HashSetandIntegertype):Output: