Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Merging 2 sorted arrays (edge cases2)

public static int[] mergeArrays(int[] myArray, int[] alicesArray) {

    // set up our mergedArray
    int[] mergedArray = new int[myArray.length + alicesArray.length];

    int currentIndexAlices = 0;
    int currentIndexMine   = 0;
    int currentIndexMerged = 0;

    while (currentIndexMerged < mergedArray.length) {

        boolean isMyArrayExhausted = currentIndexMine >= myArray.length;
        boolean isAlicesArrayExhausted = currentIndexAlices >= alicesArray.length;

        // case: next comes from my array
        // my array must not be exhausted, and EITHER:
        // 1) Alice's array IS exhausted, or
        // 2) the current element in my array is less
        //    than the current element in Alice's array
        if (!isMyArrayExhausted && (isAlicesArrayExhausted
                || (myArray[currentIndexMine] < alicesArray[currentIndexAlices]))) {

            mergedArray[currentIndexMerged] = myArray[currentIndexMine];
            currentIndexMine++;

        // case: next comes from Alice's array
        } else {
            mergedArray[currentIndexMerged] = alicesArray[currentIndexAlices];
            currentIndexAlices++;
        }

        currentIndexMerged++;
    }

    return mergedArray;
}
Comment

PREVIOUS NEXT
Code Example
Java :: HQL . getFirstResult() 
Java :: get time until start of next hour in java 
Java :: randpm years java 
Java :: is type java 
Java :: firebase timestamp to textview 
Java :: how to clear the consol after input java 
Java :: Java pollfirst() and pollLast() Methods 
Java :: password = sdf345 password.isalpha() java 
Java :: java ordenar numeros ascendente 
Java :: sudoku generator java code 
Java :: Sample TreeMap 
Java :: print current user roles in java 
Java :: type casting 
Java :: java Map to LinkedHashMap with ascending order of keys 
Java :: java ordenar los valores de un array de menor a mayor 
Java :: how to convert a jsonobject to a dbobject 
Java :: spigot self cancelling scheduler 
Java :: ein wort in buchstaben zerlegen java 
Java :: Meeting time: Merging Ranges (return) 
Java :: what is assignment in java with example 
Java :: Txt to Json in java 
Java :: jsonobject add key value java 
Java :: how to put all words from a file in an array java 
Java :: Java remove element in a array - set to null 
Java :: enhanced 4 loop 
Java :: multipleQuastion.Java 
Java :: change button background drawable in code 
Java :: Java program to find largest of three numbers using nested if 
Java :: bluetooth chat example android server 
Java :: Draw an item using System.out.println 
ADD CONTENT
Topic
Content
Source link
Name
7+4 =