Not able to give inputs to a function java. -
in example, want add contents of 2 arrays, not able how should input given. example, in case, "invalid assignment operator" error showing up, line int[] = new int[1,2]. want know how call function addarr, using arrays , b.
public class arradd {
public static void main(string[] args) { // todo auto-generated method stub int[] = new int[1,2]; int[] b = new int[3,4]; new arradd.addarr(a,b); } public void addarr(int[] arr1, int[] arr2){ int total = 0; for(int = 0; < arr1.length; i++){ total += arr1[i]; } for(int = 0; < arr2.length; i++){ total += arr2[i]; } system.out.println(total); }
}
one of problems why "invalid assignment operator" error showing because can't in java
int[] =new int[1,2]; // give compiler error.
the parameter inside square bracket means size of array.
int[] a=new int[2];
here 2 means size of array 'a'.
if want declare contents array, this
int[] a=new int[2]{1,2};
this need ...where value inside square bracket tells compiler size of array 'a' , values inside curly braces tells contents 2 contents of given array 'a'.
and dont need new operator or class name call method within class.
addarr(a,b);
to call function , function invoked.
Comments
Post a Comment