Lets say I input array {1,2,3,4} and I want to make a new one {1,2,3,4,4,3,2,1}.
import java.util.Scanner;
public class Task2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] arr = new int[4];
for(int i = 0; i < arr.length; i++){
System.out.println("Please enter number!");
arr[i]=sc.nextInt();
}
int[] actualArr = new int[arr.length*2];
for(int j = 0; j < arr.length; j++){
actualArr[j]=arr[j];
}
for (int k = arr.length - 1; k >= 0; k--) { //problem loop
actualArr[arr.length] = arr[k];
}
for (int g = 0; g < actualArr.length; g++) {
System.out.print(actualArr[g] + " ");
}
}
}
Why do I get 1 2 3 4 1 0 0 0 ? My int k starts from 3 but I get the [0] index from int[]arr which is 1 and not the last which is 4, also how can i copy the rest of the first array in reverse order?