package ru.job4j.array;

/**
 * Class Class for create sorted array from two sorted arrays.
 * @author atrifonov
 * @since 23.07.2017
 * @version 1
 */
public class ArrayFromTwoArrays {
    /**
     * Sort int array from items first array and items second array.
     * @param first Sorted int first array.
     * @param second Sorted int second array.
     * @return Sorted int array from items first arrays and items second arrays.
     */
    public int[] arrayFromTwoSortedArrays(int[] first, int[] second) {
        int j = 0;
        int k = 0;

        int firstLength = first.length;
        int secondLength = second.length;
        int[] resultArray = new int[firstLength + secondLength];

        boolean firstFinish = false;
        int i ;
        for (i = 0; i < firstLength + secondLength; i++) {
            if (first[j] <= second[k]) {
                resultArray[i] = first[j];
                if (j != firstLength - 1) {
                    j++;
                } else {
                    firstFinish = true;
                    break;
                }
            } else {
                resultArray[i] = second[k];
                if(k != secondLength - 1) {
                    k++;
                } else {
                    break;
                }
            }
        }

        for(i = i + 1; i < firstLength + secondLength; i ++) {
            if (firstFinish) {
                resultArray[i] = second[k];
                k++;
            } else {
                resultArray[i] = first[j];
                j++;
            }
        }

        return resultArray;
    }
}
@@ -742,14 +742,36 @@
     
-int i 
+boolean secondFinish = false
 ;%0A  
@@ -781,16 +781,20 @@
    for (
+int 
 i = 0; i
@@ -842,24 +842,228 @@
          if 
+(firstFinish) %7B%0A                resultArray%5Bi%5D = second%5Bk%5D;%0A                k++;%0A            %7D else if(secondFinish) %7B%0A                resultArray%5Bi%5D = first%5Bj%5D;%0A                j++;%0A            %7D else if
 (first%5Bj%5D %3C=
@@ -1137,17 +1137,16 @@
       if
- 
 (j != fi
@@ -1256,35 +1256,8 @@
 ue;%0A
-                    break;%0A
     
@@ -1453,301 +1453,46 @@
     
-break;%0A                %7D%0A            %7D%0A        %7D%0A%0A        for(i = i + 1; i %3C firstLength + secondLength; i ++) %7B%0A            if (firstFinish) %7B%0A                resultArray%5Bi%5D = second%5Bk%5D;%0A                k++;%0A            %7D else %7B%0A                resultArray%5Bi%5D = first%5Bj%5D;%0A                j++;
+secondFinish = true;%0A                %7D
 %0A   

edit task after control question chapter_001