# Need an Idea: storing elements of two different arrays?

• May 12th, 2012, 03:17 PM
red7
Need an Idea: storing elements of two different arrays?
Heres what I got so far.

1. Size of both array.
2. An object array that has the size of both combined

How would I pass all the elements of both array into that new object?.

ex:
Array 1 would have : 1,2,3,4,5,6;
Array 2 would have : 7,8,9;

How would I get
Array 3 to have: 1,2,3,4,5,6,7,8,9;
• May 12th, 2012, 03:34 PM
Norm
Re: Need an Idea: storing elements of two different arrays?
If the data types are compatible, copy the two arrays into the third array.
You could use one of the Arrays class's copy methods for one and a loop for the other.
• May 12th, 2012, 04:21 PM
red7
Re: Need an Idea: storing elements of two different arrays?
How would I use a for loop to accomplish this?
temp = Array 1 length + Array 2 length
I would have:
for(int i = 0; i<=temp;i++)
Stuck there
• May 12th, 2012, 04:28 PM
Norm
Re: Need an Idea: storing elements of two different arrays?
To get an idea of how the indexes would go, take a piece of paper and draw the slots for the third array to receive the contents of the other two. Then beneath it, draw the slots for the two shorter arrays that are to be copied. Write down the source indexes and the target indexes for each array as you would copy the contents from one to the other.
You should see that there are more than one index value being used. For the first array, the source and target indexes will be the same. for the second array, they will be different.
• May 12th, 2012, 05:21 PM
red7
Re: Need an Idea: storing elements of two different arrays?
Wow, couldn't believe how clear it was when writing it down, feel kinda stupid now haha. Finally got it to work, much much thanks, problem solved.
• May 12th, 2012, 05:29 PM
Norm
Re: Need an Idea: storing elements of two different arrays?
I used paper and pencil a lot for arrays and linked lists. It's good to see all the details visually before trying to write any code.