I'm reading Java complete reference book, there I encountered following code example, I was expecting the output to be AAAAAAAAAA (10 Times) but I got the array right rotated by one.
In this thread devs are saying that
The difference is that Arrays.copyOf does not only copy elements, it also creates a new array. System.arraycopy copies into an existing array.
but the output saying something else.
public class TestClass {
public static void main(String[] args) {
byte a[] = { 65, 66, 67, 68, 69, 70, 71, 72, 73, 74 };
System.arraycopy(a, 0, a, 1, a.length - 1);
System.out.println(new String(a));
}
}
why i'm expecting AAAAAAAAAA
pick A at 0, put A at 1 -> AACDEFGHIJ
pick A at 1, put A at 2 -> AAADEFGHIJ
....
......
PS: turns out System.arraycopy can be used to efficiently rotate(shift) an array of any type.