I am trying to shift the elements of an array left by n (some random number) using a method. The difficulty is with swapping the elements at the start of the array with the elements at the end. Here is my code so far.
The other array questions asked were different from this one.
public class Shifty {
private int[] datas = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
public Shifty() {
datas = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
}
public int shift(int position) {
int tmp = 0;
int[] tmp1 = new int[12];
for (int i = 0; i <= 12; i++) {
if (i < position) {
tmp1[12-position] = this.datas[i];
} else {
this.datas[i] = this.datas[i+1];
}
for (int j = 12-position; j <= 12; j++){
this.datas[j] = tmp1[j];
}
}
return position;
}
public void display() {
System.out.println(Arrays.toString(this.datas));
System.out.println();
}
}