0
package learningphase;

public class reversearray {
    public void perform(int arr1[]) {
        int start=0;
        int end = arr1.length;
        int temp;
        while(start<end) {
            temp=arr1[start];
            arr1[start]=arr1[end];
            arr1[end]=temp;
            start++;
            end--;
        }
         
    }
    void printreversearray(int arr1[]) {
        for(int i=0;i<arr1.length;i++) {
            System.out.println(arr1[i]);
        }
        
    }
    public static void main(String args[]) {
        reversearray reversearray1 = new reversearray();
        int arr1[]= {1,2,3,4,5};
        
        reversearray1.perform(arr1);
        reversearray1.printreversearray(arr1);
    }

}

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5 at learningphase.reversearray.perform(reversearray.java:10) at learningphase.reversearray.main(reversearray.java:27)

jps
  • 20,041
  • 15
  • 75
  • 79
abhishek tyagi
  • 1
  • 1
  • 1
  • 2

1 Answers1

1

Update the end of the array to the following

int end = arr1.length - 1;
0xh3xa
  • 4,801
  • 2
  • 14
  • 28