java - Adding jagged arrays beginner -
i'm beginner @ java @ struggling this:
i trying sum 2 jagged arrays ( n , m, both double [][]) of same size (each length 3 @ first level, of length x-1,x , x-1 respectively @ second level).
the problem i'm having specify length each array within jagged array should be, @ moment code producing n x n array because i've specified length n[1] rather parameter, if try , use sum[i].length=n[i].length error, "cannot assign value final variable". know part wrong don't know right...
thanks help!
my code:
else if (isvalidtridiagonal(m)== true && isvalidtridiagonal (n) == true) { int size = n[1].length; /** specifying lengths x shouldnt be*/ sum = new double[3][size]; (int = 0; < n.length; i++) { for(int j = 0; j< n[i].length; j++) { sum [i][j]= n[i][j] + m [i][j]; } } return sum;
}
there missing information. far can tell there 2 things need fix. seem have "sum" final variable defined in code.
secondly, declaring new array 3xsize big. if want jagged array in sence, must leave 1 of brackets empty , in first loop insert new array of wanted size.
double[][] sum = new double[3][]; //make sure unique within scope for(int = 0; < 3; i++) { //if want dynamic scaling you'll need replace 3 in array well. int size = n[i].length; //size of new row sum[i] = new double[size]; // inserting new array of wanted size for(int j = 0; j< sum[i].length; j++) { sum[i][j]= n[i][j] + m[i][j]; } } return sum;
Comments
Post a Comment