+ 1
How can I check whether an index contain a value?
int[] [] matrix={{1,2,3},{9,8,6},{7,5}} Suppose I have this matrix and the last index[2][2] doesn't have any value. How can I check this with an if selection statement?
3 Respuestas
+ 5
If each row must strictly have 3 elements then you can check the particular row `length` property - whether it has less than 3 elements. But actually this only checks for column count, if you want to check matrix[2][1] for example, this won't help.
if (matrix[2].length < 3)
// your code here
Perhaps you can try to reference a column from a particular row, and wrap the code in a try-catch block - so you can survive an exception. When an exception is raised, you know that particular column of a row is unavailable, just an idea : )
+ 2
I don't think that's possible though. The array `matrix` is statistically allocated, and as such, not expandable once created. I'm not sure what to suggest, but you need a dynamic container for that purpose, I guess.
+ 1
Thanks for the answer. But what if I want to fill the index[2][2] with a 0 value for example
From:
int[] [] matrix={{1,2,3},{9,8,6},{7,5}}
To:
int[] [] matrix={{1,2,3},{9,8,6},{7,5,0}}
How can I do this?