3 Replies - 37044 Views - Last Post: 02 April 2008 - 11:15 AM Rate Topic: -----

#1 chetah  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 111
  • Joined: 17-November 07

Merging two arrays in Java

Post icon  Posted 02 April 2008 - 10:28 AM


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[i]);
		}
		
	}//end main
}//end class



I am trying to merge the two arrays above. I am getting an out of range error, what might the problem be?

Is This A Good Question/Topic? 0
  • +

Replies To: Merging two arrays in Java

#2 gl3thr0  Icon User is offline

  • D.I.C Regular

Reputation: 19
  • View blog
  • Posts: 319
  • Joined: 27-October 07

Re: Merging two arrays in Java

Posted 02 April 2008 - 10:58 AM

you get this error because of this part here
B[bIndex];
this loop is set to keep going while bIndex < cIndex so wt ends up happening is bIndex becomes 3&4 which is bigger than B[] .. understand??


so in order to fix this we have 2 decrease the size of bIndex when u access the B[]
todo that we will change this line here
C[bIndex] = B[bIndex]; to C[bIndex] = B[bIndex-A.length];

now this still wont work because the way you have set up ur for loop is wrong bIndex shouldnt start off a 1 it needs to continue from where aIndex left off which is actually 2.. so if we want to hardcode it cange it 2
for(int bIndex = 2; bIndex < cIndex; bIndex++){
bt for better results use
for(int bIndex = aIndex; bIndex < cIndex; bIndex++){



its all fixed.... while no the problems you were having are fixed but the code is still shit. if you change the array values or add more elements 2 the first array then it crashes :(
+ ur doing this is 2 loops! y? and have bunch of un needed variables
ill give u a quick example but try and write the code urself :P
for(keep going while i < c.length)
	   if i is smaller than a.length
			//c =a
	   if i is bigger or the same as a.length
		   c = b-a.length



should look something like this try it out

This post has been edited by gl3thr0: 02 April 2008 - 11:15 AM

Was This Post Helpful? 0
  • +
  • -

#3 corliss  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 189
  • Joined: 25-October 06

Re: Merging two arrays in Java

Posted 02 April 2008 - 11:06 AM

Your are moving beyond the address of the the array.
example.
int[]B={2,3,4}
In simple terms
there are only 3 "slots" in this array
so check to see if the there is an address before trying to look at the data.

This post has been edited by corliss: 02 April 2008 - 11:10 AM

Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4421
  • View blog
  • Posts: 12,286
  • Joined: 18-April 07

Re: Merging two arrays in Java

Posted 02 April 2008 - 11:15 AM

gl3thr0 hit the nail on the head here but I thought I would chime in with some addition information and a quick hack that may help you out.

public 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;

        // Copy over array A (but only if the element is less than B)/>
        // What if it isn't less than B?

        while (aIndex < aCount){
    		if(A[aIndex] < B[aIndex]){
    		     C[aIndex]= A[aIndex];
    		}
    		aIndex++;
        
        }//end while
        

        // Copy over B starting where array A left off
        // But be sure to read the part about copying A above.

        for(int bIndex = 0; bIndex < B.length; bIndex++){
    		C[aIndex] = B[bIndex];                
    		aIndex++;
        }
        
        for (int i = 0; i < C.length; i++){
            System.out.println(C[i]);
        }
        
    }//end main
}//end class



While this will work with your current situation, you may want to check out the first part again about copying over A. You have an if statement in there saying that it will only copy over elements from A if they are less than B. But what if they are not less than B? This will result in an element being skipped in array C (since you increment aIndex despite the copy over was successful or not) and could cause problems.

So check that out and see if that is exactly what you want. :)

Enjoy!

This post has been edited by Martyr2: 02 April 2008 - 11:17 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1