java - How to divide a series of values in one array by a series of values in another array -


i beginner in java , ask array problem having. trying build simple program has 2 int type arrays 5 integers in each array. want divide length of integers in 1 array length of integers in other array. program seems work extent, since gives me results of divisions. gives me following error:

exception in thread "main" java.lang.arrayindexoutofboundsexception: 5 @ arraydivide.main(arraydivide.java:11)

can tell me once going wrong? here code:

public class arraydivide {      public static void main(string[] args) {          int arr1[]={8,4,6,8,4};         int arr2[]={2,4,2,1,2};          (int x =0;x <arr1.length;x++){             (int j =0;x <arr2.length;j++){                 int result = arr1[x] / arr2[j];                   system.out.println(result);              }         }     }        } 

divide each number of outer array (arr1) respective number in other array(arr2)

the code shown below. should put check verify both arrays have same length.

public static void main(string[] args) {         int arr1[]={8,4,6,8,4};         int arr2[]={2,4,2,1,2};          (int x =0;x <arr1.length;x++){                 int result = arr1[x] / arr2[x];                 system.out.println(result);         }     } 

output is:

4 1 3 8 2 

divide each number of outer array(arr1) each number in other array (arr2)

and if want divide each number of outer array each number of inner array use below code. in code condition not right, should j <arr2.length , not x <arr2.length.

public static void main(string[] args) {         int arr1[]={8,4,6,8,4};         int arr2[]={2,4,2,1,2};          (int i=0; i<arr1.length; i++){             (int j =0;j<arr2.length;j++){                 int result = arr1[i] / arr2[j];                  system.out.println(result);              }         }     } 

Comments