2

I wanted to copy array from one Integer array into another array many times.

int a[6]={1,2,3};
int b[]=new int[12];
for(int i=0;i<12;i++)
{
b[i]=a[i];
System.out.println(b[i]);
}

I wanted output like this:

1,2,3,1,2,3,1,2,3,1,2,3

How should i copy all element from a[] to b[] for as many times i want.

5
  • You are aware that this code does not work yet? You are accessing the index of a[6] for example and that exceeds the size of the array. Also your array initialization is not correct. Now to prepare a good answer: Do you really need the println output? This will prevent batch copies. Commented Jun 25, 2015 at 12:53
  • You didn't got my question Sir. Commented Jun 25, 2015 at 13:01
  • Please have a look at my answer. This may clarify why I was asking. Commented Jun 25, 2015 at 13:04
  • int a[6]={1,2,3}; int b[]=new int[12]; for(int i=0;i<12;i++) { b[i]=a[i]; System.out.println(b[i]); } I wanted output like this 1,2,3,1,2,3,1,2,3,1,2,3 and i dont know how to do this Sir. thats why I was asking. And The initialization was correct. Commented Jun 25, 2015 at 13:14
  • You got two answers. If you want the println output Erans answer is the way to go. That doesn't work with my proposal. If you want to transfer the data from array a to b both answer work. Commented Jun 25, 2015 at 13:17

2 Answers 2

6

You can use modulus operator :

for(int i=0;i<12;i++)
{
    b[i]=a[i%a.length];
    System.out.println(b[i]);
}

i%a.length will iterate repeatedly from 0 to a.length-1.

Sign up to request clarification or add additional context in comments.

2 Comments

The simplest and closest to OP's approach. +1
if you pull a.length out of the loop it will also decrease that latency which could matter if this is being done at high enough iterations.
1

The most efficient way to do what you want (that I can think of) is to copy batches.

So we got our two input arrays:

int a[] = {1, 2, 3};
int b[] = new int[12];

What we want to do is to copy the first array into the second until again and again in sequence until the second array is filled.

The first batch is simple. Just copy the first array once into the second.

System.arraycopy(a, 0, b, 0, a.length);

And now we copy what we append the data in the second array to the free fields of the second array.

int usedSize = a.length; 
while (usedSize < b.length) {
    System.arraycopy(b, 0, b, usedSize, Math.min(b.length - usedSize, usedSize);
    usedSize *= 2; 
}

Your array b will change in the following way:

{1,2,3,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x}
{1,2,3,1,2,3,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x,x}
{1,2,3,1,2,3,1,2,3,1,2,3,x,x,x,x,x,x,x,x,x,x,x,x}
{1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3}

So it reduced the copy step required and will work very fast even for big arrays. System.arraycopy is in the end the fastest way to copy array contents around.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.