Copy array into another array many times in java -
i wanted copy array 1 integer array 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 this:
1,2,3,1,2,3,1,2,3,1,2,3
how should copy element a[] b[] many times want.
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
iterate repeatedly 0 a.length-1.
Comments
Post a Comment