0

I want to compare two string arrays A, B. I want to return and/or display the elements unique to A, B and those which are in both A & B. I called my method as shown below, but I get wrong results. How to fix it ?

A = 1,2,3,4,5;
B = 1,2;
compareStringArray(A,B, true);// true means print results

Results -

--Elements in ONLY A - 

3, 5

--Elements in ONLY B - 


--Elements in both A & B - 

2

Code-

public static ArrayList<ArrayList<String>> compareStringArray(
        String[] arrayA, String[] arrayB, boolean display) {

    ArrayList<ArrayList<String>> results = new ArrayList<ArrayList<String>>();
    ArrayList<String> ara = new ArrayList<String>(Arrays.asList(arrayA));
    ArrayList<String> arb = new ArrayList<String>(Arrays.asList(arrayB));
    ArrayList<String> common = new ArrayList<String>();

    for(String s : ara){
        if (arb.contains(s)) {
            common.add(s);
        }// if

    }//for

    for(String s: common){
        if (ara.contains(s)) {
            ara.remove(s);
        }// if

        if (arb.contains(s)) {
            arb.remove(s);
        }// if

    }//for


    results.add(ara);
    results.add(arb);
    results.add(common);

    if (display == true) {

        ArrayList<String> als = null;

        als = results.get(0);
        System.out.println("\n--Elements in ONLY A - \n");
        printArrayListOfStringAsCSV(als);

        als = results.get(1);
        System.out.println("\n--Elements in ONLY B - \n");
        printArrayListOfStringAsCSV(als);

        als = results.get(2);
        System.out.println("\n--Elements in both A & B - \n");
        printArrayListOfStringAsCSV(als);

    }// if

    return results;

}// compare
4
  • 1
    use nested loop ,in first time choose the first arg of arr1 and go through next arr which is arr2 Commented Dec 7, 2013 at 21:03
  • Can we see what results you are getting? Possible .contains(String) does not work for the same reason you use .equals() to compare strings and not == but I could be way off on that Commented Dec 7, 2013 at 21:04
  • @ghostbust555 - looks like you did not notice. The results have already been given ! Commented Dec 7, 2013 at 21:05
  • Oh wow you are correct. I read too quickly and assumed those were the desired results not the current reuslts Commented Dec 9, 2013 at 17:38

2 Answers 2

1

Your code is working on my machine (I had to fix main, and implement printArrayListOfStringAsCSV so tsk-tsk) -

public static void main(String[] args) {
  String[] A = { "1", "2", "3", "4", "5" };
  String[] B = { "1", "2" };
  compareStringArray(A, B, true);
}

public static void printArrayListOfStringAsCSV(
    List<String> al) {
  for (int i = 0; i < al.size(); i++) {
    if (i != 0) {
      System.out.print(", ");
    }
    System.out.print(al.get(i));
  }
  System.out.println();
}

And I get this output (which looks 100% to me).

--Elements in ONLY A - 

3, 4, 5

--Elements in ONLY B - 



--Elements in both A & B - 

1, 2
Sign up to request clarification or add additional context in comments.

3 Comments

Yes, my stupidity :( . It was a problem with my printArrayListOfStringAsCSV method. Old method - public static void printArrayListOfStringAsCSV(ArrayList<String> results){ String csv = ""; int size = results.size(); if(size > 1){ int lastIdx = size - 1; for(int i = 0; i < lastIdx-1; i++){ csv = csv + results.get(i) + ", "; } csv = csv + results.get(lastIdx); System.out.println(csv); }//if }//print
in the for loop, lastIdx-1 is to be replace by lastIdx-1.
btw, i think that using if check for comma is a not good. Iterate up to last index and then print last element after that.
1

It probebly means that the problem is in printArrayListOfStringAsCSV(). Look into it...

1 Comment

yeah, my stupidity. i never thought i could make a mistake there :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.