Tuesday, 20 March 2018

copy content of two arrays into seperate array



public class TestLooping {

public static void main(String[] args) {

                //first array
                int[] firstArray = {2,4,6,8,10};

                //second array
                int[] secondArray = {12,14,16,18,20};

                //third array, size of third array is sum of firstArray length and secondArray length
                int[] thirdArray = new int[firstArray.length + secondArray.length];

                //copying elements of first array to third array
                for(int i = 0 ; i < firstArray.length ; i++) {
thirdArray[i] = firstArray[i];
}

                // copying elements of second array to third array, index of third array is added with length                  // of first array
for(int i = 0 ; i < secondArray.length ; i++) {
thirdArray[i+firstArray.length] = secondArray[i];
}

                // print all the elements of third array
for(int i=0; i<thirdArray.length;i++) {
System.out.println(thirdArray[i]);
}

}

}