comparing two string arrays using java -
we trying compare 2 string arrays( as[ ] , bs[ ]) , update array string as[ ] new strings present in bs[ ] .we not able update as[ ].pls following codes.thank u;)
public class aa { /** * @param args command line arguments */ public static void main(string[] args) { // create array of 4 strings (indexes 0 - 3) string as[] = new string[5]; string bs[] = new string[16]; int i; try { // create bufferreader object read our file with. bufferedreader reader = new bufferedreader(new filereader("input.txt")); bufferedreader reader1; reader1 = new bufferedreader(new filereader("a1.txt")); // line hold our line read file string line = ""; string line1 = ""; // counter keep track of how many lines have read int counter = 0; int counter1 = 0; // read in line file , store in "line". while don't hit null or while counter less 4. // counter prevents reading in many lines. while (((line = reader.readline()) != null) && (counter < 4)) { as[counter] = line; counter++; } while (((line1 = reader1.readline()) != null) && (counter1 < 16)) { bs[counter1] = line1; counter1++; } system.out.println("value"+as[0]); system.out.println("value"+bs[0]); int temp,temp1,j; temp=as.length; temp1=bs.length; system.out.println("length:"+temp); system.out.println("length1:"+temp1); for(i=0;i<bs.length;i++) { for(j=0;j<as.length;j++) { if(as[j].equals(bs[i])) { //ignore } else { temp++; as[temp]=bs[i]; } } } // foreach style loop loop through array of strings , print them out show read in. reader1.close(); reader.close(); } catch (exception ex) { system.out.println("exception: " + ex.getmessage()); } } }
since using 2 arrays containing strings, better convert both list , add
list alist = (arrays.aslist(as)); list blist = (arrays.aslist(bs)); blist.removeall(alist); // assuming have common objects in both alist.addall(blist); = alist.toarray(); // convert array