简体   繁体   中英

Merge two String Arrays using only loops

I'm having some issues with a simple task that I'm not being able to understand. I've got a simple task which is to create an alghoritm that merges two arrays but without repetitions only using for loops...

This is my code so far...

public static String[] mergeReps(String[] a1, String[] a2) {

    // Create a new array with the full size (I'll remove the nulls later)
    String[] retArray = new String[a1.length + a2.length];

    // Copy of array a1 to the retArray
    retArray = Arrays.copyOf(a1, a1.length);

    // Test print...
    System.out.println(Arrays.toString(retArray));

    // loop to check if the indexes value are present in the a1 array
    for (int i = 0; i < a1.length - 1; i++) {
        for (int j = i + 1; j < a2.length; j++) {
            // Condition to check if the value is duplicated
            if (!(a1[j].equalsIgnoreCase(a2[i]))) {
                retArray[i + a1.length] = a2[j];
            }
        }
    }
    return retArray;
}

My question is: How can I compare a2[0] to every single position in the a1 array, and only after doing that and knowing if it's duplicate or not, add it to the retArray?

Try this code Snippet. Basically it uses the uniqueness property of a set to compile a set of unique string values. It then converts it to String[].

Check out the javadocs for HashSets for more information on how they work.

  Set<String> result = new HashSet<String>();
  for(String s : a1)
  {
     result.add(s);
  }
  for(String s : a2)
  {
     result.add(s)
  }

  return result.toArray(new String[result.size()]);

You can use this:

for(int nextItemOfSecondArray = a2.length -1; nextItemOfSecondArray >= 0; i--){

    boolean checkIfAlreadyExists = false;

    for(int i = 0; i < a2.length; i++){

        if(a2[nextItemOfSecondArray].equalsIgnoreCase(a1[i])){
            checkIfAlreadyExists = true;
            break;
        }
    }

    if(!checkIfAlreadyExists)
        a1[a1.length] = a2[nextItemOfSecondArray];

}

Assuming a1 and a2 each have no duplicates, here is how to merge the arrays without duplicates:

public static String[] mergeReps(String[] a1, String[] a2) {

    //validate the incoming arrays
    if (a1 == null && a2 == null) {
        System.err.println("error - input arrays are null");
        return new String[]{};
    } else if (a1 == null) {
        return a2;
    } else if (a2 == null) {
        return a1;
    }

    int arrEffSize = 0;
    boolean unique;
    String s, sfinal;
    String[] tempArray = new String[a1.length + a2.length];

    //just copy a1 to the tempArray
    System.arraycopy(a1, 0, tempArray, 0, a1.length);
    arrEffSize = a1.length;

    //add String objects from a2, if it isn't already there
    for (int i=0; i < a2.length; i++) {
        unique = true;
        s = a2[i];
        for (int j=0; j < arrEffSize; j++) {
            sfinal = tempArray[j];
            if (s.equalsIgnoreCase(sfinal)) {
                unique = false;
                break;
            }
        }
        if (unique) {
            tempArray[arrEffSize] = s;
            arrEffSize++;
        }
    }   

    //create a new array with the appropriate size, then copy tempArray to it
    String[] retArray = new String[arrEffSize];
    System.arraycopy(tempArray, 0, retArray, 0, retArray.length);

    return retArray;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM