-1
public class Test {
    public static void main(String[] args){

        int array1[] = {5,8,11,1,6};
        int array2[] = new int[5];

        for(int transfer=4; transfer<array1.length; transfer--){
            array2=array1;
            /*this is line 9*/System.out.print(array2[transfer] + " ");
        }

    }
}

OUTPUT:

6 1 11 8 5 Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at Test.main(Test.java:9)

ChrisMM
  • 5,195
  • 8
  • 19
  • 34
J.E.T.
  • 9
  • 1
  • 1
    By far, the best way to understand where your code is going wrong is to use the debugger built into your IDE to step through your code statement by statement, watching the values of your variables. Using a debugger isn't just for advanced coders. It's fundamentally the next thing you should learn how to do after "Hello, world." – T.J. Crowder Dec 06 '19 at 15:49
  • Does this answer your question? [Make copy of an array](https://stackoverflow.com/questions/5785745/make-copy-of-an-array) – Kyle Swanson Dec 06 '19 at 18:37

4 Answers4

2

Your program runs as long as transfer is smaller than the length of the first array, and it starts at, which is already smaller, and gets reduced by 1 each turn, so it will naturally try to access array2[-1].

Additionally, you have to use

array2[transfer] = array1[transfer]

instead of your current transferring, as this copies the whole array at once.

Fasde
  • 90
  • 12
  • 1
    *"...as this copies the whole array at once."* It doesn't **copy** the array, it just makes the variable point to the array, so both variables are pointing to the same array. – T.J. Crowder Dec 06 '19 at 15:53
0

transfer starts at 4 and all your code ever does is decrement it (make it smaller, 4 to 3, 3 to 2, etc.). Your loop continues as long as transfer<array1.length is true. Since array1.length is 5, and 4 (and 3, and 2, ...) is less than 5, the loop will continue forever or, in this case, until you try to access the entry at index -1 because transfer has been decremented five times.

T.J. Crowder
  • 879,024
  • 165
  • 1,615
  • 1,639
  • I think my question is not clear, I apologize for that. What I mean is that, I want to transfer the elements of Array1 to Array2 and print it in reverse. This is what I've come up with: int array1[] = {5,8,11,1,6}; int array2[] = new int[5]; for(int transfer=4; transfer – J.E.T. Dec 06 '19 at 16:33
  • @J.E.T. - If one of the answers resolved your issue, you can help the community by marking it as accepted. An accepted answer helps future visitors use the solution confidently. Check https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work to learn how to do it. – Arvind Kumar Avinash May 29 '20 at 16:23
0

The issue with your code is that the loop counter is running in the backward direction because of transfer-- and when it's reaching -1, trying to access array2[-1] is throwing the ArrayIndexOutOfBoundsException because the lowest index in an array is 0.

By the way, you can transfer elements from one array to another using System.arraycopy easily as follows:

import java.util.Arrays;

class Main {
    public static void main(String[] args) {
        int[] array1 = { 1, 2, 3, 4, 5, 6 };
        int[] array2 = { 7, 8, 9, 10, 11, 12 };
        System.out.println(Arrays.toString(array1));
        System.out.println(Arrays.toString(array2));
        System.arraycopy(array1, 2, array2, 2, 3);
        System.out.println(Arrays.toString(array2));
    }
}

Output:

[1, 2, 3, 4, 5, 6]
[7, 8, 9, 10, 11, 12]
[7, 8, 3, 4, 5, 12]

Update: From your comment, I understand that you wanted to copy elements from array1 to array2 in reverse order. Given below is the code for the same:

class Main {
    public static void main(String[] args) {
        int[] array1 = {5,8,11,1,6};
        int[] array2 = new int[5];

        // Copy elements from array1 to array2 in reverse order
        for(int i=0;i<array1.length;i++) {
            array2[i]=array1[array1.length-1-i];
        }

        //Print elements of array2
        for(int n:array2) {
            System.out.print(n+" ");
        }
    }
}
Arvind Kumar Avinash
  • 50,121
  • 5
  • 26
  • 72
0

You can use Arrays to to copy values in a new array like this :

int array1[] = {5,8,11,1,6};
int array2[] = Arrays.copyOf(array1, array1.length);
for (int i = 0; i< array1.length; i++) System.out.print(array2[i] + " ");
kevin ternet
  • 3,779
  • 2
  • 14
  • 23