I have seen other solutions like this and wondering if there is anything wrong with my approach of using for loop. I don't want to use while loop as others have used in their solution.
package com.my.practice;
public class MedianOfTwoArrays {
public static void main(String[] args) {
// Given two sorted arrays of same size
int[] a1 = {1,2,3,4,5,6};
int[] a2 = {7,8,9,10,11,12};
int[] mergedArray = new int[a1.length + a2.length];
for(int i=0 ; i < a1.length; i++){
mergedArray[i] = a1[i];
}
//System.out.println("Length:"+2*(a1.length));
for(int i= a1.length; i < 2 * (a1.length); i++) {
mergedArray[i] = a2[i];
}
for(int i=0 ; i < 2*(a1.length); i++){
System.out.println("Part of Array: "+mergedArray[i]+ " Length is: "+mergedArray.length);
}
}
}
I am getting following error :
Length:12
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at com.my.practice.MedianOfTwoArrays.main(MedianOfTwoArrays.java:30)