0
Got this error
public class FindError { public static void main(String[] args) { int[][] matrix = {{1}, {2}, {3}, {4}, {5}, {6}}; System.out.println(m(matrix, 1)); //error: incompatible types: int[][] cannot be converted to int } public static int m(int matrix, int column) { int sum = 0; for (int i = 0; i < matrix.length; i++) //error: int cannot be dereferenced sum += matrix[i][column]; //error: array required, but int found return sum; } }
12 Respostas
0
in public static int m the parameter matrix you must change it in int matrix[ ]
0
so
public static int m(int matrix [ ] [ ], int column)
0
Thank you, So how about the int[][]cannot be converted to int error?
0
because only int is a variable that stores a number,
int [ ] [ ] is a matrix with many numbers, so in the function you wrote int matrix but matrix isn't an int variable, it is an int [ ] [ ] variable so you pass all numbers that it stores
0
Sorry so what should i change?
0
the type of first parameter to int matrix [ ] [ ]
0
It show the same error:(
0
public class FindError {
public static void main(String[] args) {
int[][] matrix = {{1}, {2}, {3}, {4}, {5}, {6}};
System.out.println(m(matrix, 6));
}
public static int m(int matrix [][], int row) {
int sum = 0;
for (int i = 0; i < row ; i++)
sum += matrix[i][0];
return sum;
}
}
try this
0
I changed the "column" to "row" because the matrix is
1
2
3
4
5
6
(6 row, 1 column)
we must do the sum of this numbers,
the variable i at beginning stores 0; this indicate the row that start from 0 to 5 (row-1) so
sum+=matrix[i](the row) [0] (because it has only 1 column)
after the for loop the function return sum=21
0
Get it omg thank you thank you now i understand it thank you again may god bless you leo flameee:)))))
0
you're welcome :D
0
Java has two different types of variables: primitive and objects and only objects are reference types. The type int is a primitive and not an object. Dereferencing is the process of accessing the value referred to by a reference . Since, int is already a value (not a reference), it can not be dereferenced. Primitives (byte, char, short, int, long, float, double, boolean) are not objects and do not have member variables or methods. They're just simple values . So you cannot do somePrimitive.something().
http://net-informations.com/java/err/dereferenced.htm