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