1. Member
Join Date
Jun 2011
Posts
24
Rep Power
0

Array copy

Hello,

I have a question regarding copy between arrays. For instance: I have array1 = {"1","2","3"}, array2 ={"4","5","6"}. I need to copy array1 and array2 in to array3 which is empty. array3 should be { "1","2","3","4","5","6"}.

Did anyone know the logic of this?

2. Take a piece of paper and write down the two source arrays and the target array.
Mark the source of the first element to copy from and mark the first target element to receive that value.
copy elements and move both marks until all the elements of the first array are copied
Move to the second array and set a mark at its first element.
copy elements and move marks until the second array is copied.

3. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
787
Rep Power
8
#1
List<String> both = new ArrayList<String>(array1.length + array2.length);
String[] array3 = both.toArray(new String[0]);
#2
String[] array3 = new String[array1.length + array2.length];
System.arraycopy(array1, 0, array3, 0, array1.length);
System.arraycopy(array2, 0, array3, array1.length, array2.length);
#3
iterate over the first array and then over the second one :)
#4

/edit: sorry Norm ;/

4. @eRaaaaa What is the code you posted supposed to do?

5. Member
Join Date
Jun 2011
Posts
24
Rep Power
0

6. Senior Member
Join Date
Oct 2010
Location
Germany
Posts
787
Rep Power
8
#1
List<String> both = new ArrayList<String>(array1.length + array2.length); //create a arraylist with initial capacity of lenght1 and length2
Collections.addAll(both, array1); //add all strings from the first array to the list
Collections.addAll(both, array2); //add all strings from the second array to the list
String[] array3 = both.toArray(new String[0]); //"convert" the list back to an array

see the collections api doc: Collections (Java Platform SE 6)
#2
String[] array3 = new String[array1.length + array2.length]; //create a third array with the size of lenght1 and length2
System.arraycopy(array1, 0, array3, 0, array1.length); //copy all elements from array1 to the first array1.length positions in array3
System.arraycopy(array2, 0, array3, array1.length, array2.length); //copy all elements from array2 in array3, started at index array1.length
the arguments are:

Object src,
int srcPos,
Object dest,
int destPos,
int length

after the first System.arraycopy call the third array looks like:
[1,2,3,0,0,0]
so at the second call you have to change the fourth argument (destination position) to array1.length to add the other to the end otherwise you would overwrite them again!

see the system api doc: System (Java Platform SE 6)

Why not attempt at writing the code yourself. That is the best way to learn.

8. Member
Join Date
Jun 2011
Posts
24
Rep Power
0
Thanks a lot for the information. I have a doubt at code#1 of line 4. Can u tell me what does String[0] mean?

9. Java Code:
`both.toArray(new String[0]);`
The variable both refers to an ArrayList and it is calling the toArray method. Why not go to the Java API and read about that method?

10. Moderator
Join Date
Feb 2009
Location
New Zealand
Posts
4,712
Rep Power
15
Can u tell me what does String[0] mean?
The actual expression on that line is "new String[0]". This creates a String array of length zero. (See the examples of this in the Arrays page of Oracle's Tutorial).

It might seem like a silly thing to create - a container so small that nothing can be put into it. To see why it there you need to have a read of the API docs for the toArray(T[]) method of List.

Returns an array containing all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.

In other words the zero length String array is used as an argument in the toArray() call so that that method will return a String array.

-----

I cannot reccommend highly enough these two sources - the Tutorial and the API documentation - as the starting place when you want to know what code does. Of course forums like this are also good when the available documentation and examples are unclear in some way.

11. Member
Join Date
Jun 2011
Posts
24
Rep Power
0
Hello eRaaa,

I have found an error while using code#2. I have used this code in 'while' loop. When the loop iterate for the 1st time, it copies array1 and array2 into array3. When the loop iterates for the 2nd time, then the values in the array3 of iteration 1 are replaced and then writes the values obtained in 2nd iteration. Can u help me in this???

-thanks

12. When the loop iterates for the 2nd time, then the values in the array3 of iteration 1 are replaced
If you assign a new value to a variable, the old value is replaced.
For example:
int i = 1;
i = 2;
i = 3;
// Now i has the value of 3, the old values of 1 and 2 were replaced

Can you explain what you want to happen in the loop and what values you want available in the loop and after the code exits the loop?

13. Member
Join Date
Jun 2011
Posts
24
Rep Power
0
i should have 1,2,3 values.(one by one)

14. To hold multiple values in one variable: 1 and 2 and 3
you need a container of some kind.
An array or arraylist for example

15. Member
Join Date
Jul 2011
Posts
11
Rep Power
0
array1= {"1","2","3"} and array2={"4","5","6"}

array3=array1+array2 = {"1","2","3","4","5","6"}

code here .................

int array1= {"1","2","3"}
int array2={"4","5","6"}

int array3[] = new int[array1.length+array2.length];

int k=0;

for(int i=0;i<array1.length;i++)
{
array3[k] = array1[i];
k++;
}

for(int j=0;j<array2.length;j++)
{
array3[k] = array2[j];
k++;
}

16. What is that code supposed to do?
What does it do when you execute it?

Please make print outs to show what it does and add explanations about what you want different.

17. Why not use the System.arraycopy( ... ) method?

kind regards,

Jos

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•