Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Merge two 1D array's into a new 1D array. Trouble appending last index

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Merge two 1D array's into a new 1D array. Trouble appending last index

    Hello, i am having trouble appending the rest of my original array to my new array.

    I am suppose to shuffle two arrays and put the output into a new array.

    For instance.

    int [] A = {2,3,4,5};
    int [] B = {1,5,6,9,123};

    A and B are my two arrays that i need to shuffle into array C

    So C's output is:

    int [] C = {2,1,3,5,4,6,5,9,123}

    The problem i encounter is that it doesn't want to append the longer array so my output right now is

    int [] C = {2,1,3,5,4,6,5,9,0}



    my code is
                    int [] A = {2,3,4,5};
    		int [] B = {1,5,6,9,123};
     
     
     
    		int Lengthmin = (A.length<B.length? A.length:B.length);
     
    		int[] C= new int [A.length+B.length];
     
    		for(int i =0;i<Lengthmin;i++)
    		{
    			C[2*i]=A[i];
    			C[2*i+1]=B[i];
    		}
     
    //didnt include printing the array

    Thanks in advanced for any help!


  2. #2
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Merge two 1D array's into a new 1D array. Trouble appending last index

    Quote Originally Posted by norske_lab View Post
    ...it doesn't want to ...
    It does exactly what you told it to, right? It interleaves elements from the two source arrays into the target array until it gets to the end of the shorter of the source arrays, right?

    So...

    To use your program as a starting point, you can make a new loop to execute after the first loop. The counter for this new loop can start at the next index of C and will be incremented each time through the loop until all elements of C have been written. Each time it goes through the loop it gets another value from the longer of the source arrays and stores it in the target:
    Begin Loop
        If A is shorter, get the value of the next element of B and store in C
        If B is shorter, get the value of the next element of A and store in C
    End Loop

    Heck; I'll kick it off:
        for (int i = ???; i < C.length; i++) {
            C[i] = ??? // Either A[something] or B[something]
        }

    The only questions are:
    1. What is the "next index of C" that this loops starts at?
    2. What is the the source array (is it A or is it B?) and what is the index of the source array value that is copied into C[i] each time through the loop?


    If you can't figure the answers in your head, or even with pencil and paper (gasp), then put print statements inside your first loop to see what it has done by the time it gets ready for the second loop.


    Cheers!

    Z
    Last edited by Zaphod_b; August 30th, 2012 at 01:34 PM.

Similar Threads

  1. Array Index Out of bounds?
    By blazeking in forum Collections and Generics
    Replies: 1
    Last Post: March 29th, 2012, 01:44 AM
  2. Replies: 6
    Last Post: October 7th, 2011, 07:50 AM
  3. Array index out of bounds
    By fortune2k in forum Collections and Generics
    Replies: 1
    Last Post: November 30th, 2010, 06:11 AM
  4. Replies: 4
    Last Post: November 14th, 2010, 10:44 AM
  5. index of array
    By nasi in forum Java Theory & Questions
    Replies: 1
    Last Post: April 12th, 2010, 02:39 AM