Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Merging Two arrays in Java

807601Apr 2 2008 — edited Apr 2 2008
class Merge{
	public static void main(String[] args){
		int[]A = {1,2,};
		int[]B = {7,8,9};
		int[]C = new int[A.length + B.length];
		
		int aIndex = 0;
		int aCount = A.length;
		int cIndex = C.length;
		
		
		while (aIndex < aCount){
			if(A[aIndex] < B[aIndex]){
				C[aIndex]= A[aIndex];
				}
			aIndex++;
		
		}//end while
		
		for(int bIndex = 1; bIndex < cIndex ; bIndex++){
			C[bIndex] = B[bIndex];//Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
    								//at Merge.main(Merge.java:23)
	
		}
		
		for (int i = 0; i < C.length; i++){
			System.out.println(C);
}

}//end main
}//end class


I am trying to merge the two arrays above but I am getting an out of bounds error? where am I going wrong?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 30 2008
Added on Apr 2 2008
2 comments
581 views