Sign in to follow this  

transferring an array to another (JAVA)

This topic is 4689 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry for the vague title, this will be hard to explain but i will do my best: This is what I need to do: I want to take the elements from one array and copy them to another array (same size), but start at a certain definite point in the middle of the first array and 'wrap around'. For example, say I have a full array of integers with 6 elements: 0,1,2,3,4,5 in that order. I want to take the elements of that array and copy them into a second array in this order: 3,4,5,1,2. How do I go about doing this? thanks everyone. BTW i'm new here. hello. P.S. this is in Java

Share this post


Link to post
Share on other sites
This should do it. It's commented, so it should speak for itself, but feel free to ask if anything is confusing. Or badly written.

public final class Start {
public Start() {

}
public static void main(String args[]) {
System.out.println("testing entry point");
// the size is arbitrary, though naturally
// it _should_ be divisible by 2
// might need to check that
int array[] = new int[10];
for(int i = 0; i < 10; i++)
array[i] = i*i;
WrapArray(array);
}
public static void WrapArray(int src[]) {
// our destination
int[] dest = new int[src.length];
// use System.arraycopy twice
// once start at offset src.length / 2
// and again starting from the beginning
// both length = src.length / 2
// System.arraycopy(source array, offset, destination, offset, length)
System.arraycopy(src, src.length / 2, dest, 0, src.length / 2);
System.arraycopy(src, 0, dest, src.length / 2, src.length / 2);
for(int i = 0; i < 10; i++)
System.out.println(src[i] + " ");
System.out.println();
for(int i = 0; i < 10; i++)
System.out.println(dest[i] + " ");
System.out.println();
}
}



Quote:
Original post by mynameisBIRF
BTW i'm new here. hello.

Hello, and welcome. 'Tis a good place to be.

On a side note, make sure you read the GDNet Faq so you know how things work (posting links, ect).

Share this post


Link to post
Share on other sites

This topic is 4689 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this