# Need help with empty arrays

• March 23rd, 2013, 07:07 AM
robin_
Need help with empty arrays
Hello

I'm currently working on a method that is supposed to row reduce a given array. However, before I've started the coding on the row reduction. I'm trying to create a method that can control if a given array is eligble for row reduction or not.

So the thing is, I have a method that attempts to go through a given matrix [][], and checking if all elements are numbers. However this matrix below

Code :

`int [][] matrixFalse2 = {{},{},{}};`

passes through without any problem (without any numbers at all...). I don't know how to write the code so the method returns false for this kind of matrix.

Currently this code below is my current one for the number checking part,
Code :

```int elements = matrix.length*matrix[1].length; int counter = 0; for(int rows=0; rows<matrix.length; rows++){ for(int columns=0; columns<matrix[rows].length; columns++){ if(matrix[rows][columns]>=0 || matrix[rows][columns] <0){ counter = counter + 1; //System.out.println(counter + " . " + elements); just debug code } } } if(counter==elements){ return true; }else{ return false; }```

If the first row makes you wonder why it has the number 1, it's because I've already checked the matrix if the dimensions are correct, so number 1 is arbitrary in this case - feel free to ignore it.

So I was thinking of adding something like this (see below) but of course the syntax is very wrong (which is why I need help).
Code :

```if(matrix[i][j]==null){ return false; }```

And then I would loop through the matrix with the variables i & j. How am I supposed to tackle this problem?
I hope you understand my problem. Thanks in advance.

//Robin
• March 23rd, 2013, 07:22 AM
Norm
Re: Need help with empty arrays
What is the value of elements for the array you posted?
• March 23rd, 2013, 09:21 AM
robin_
Re: Need help with empty arrays
Quote:

Originally Posted by Norm
What is the value of elements for the array you posted?

What array do you mean? This one below?

int [][] matrixFalse2 = {{},{},{}};

The values are nothing, right? I'm just trying to make the user of the program not able to "crash" my program with giving it an array like this. Since this array (matrixFalse2) is an array which can't be row reduced.
And if this (matrixFalse2) passes through the test, the program will try to row reduce it, and then the program will crash.
• March 23rd, 2013, 09:26 AM
Norm
Re: Need help with empty arrays
What is the value of the variable: elements for the array you just posted?
Consider: X*0 = 0 & (0 == 0) is true
• March 23rd, 2013, 09:32 AM
robin_
Re: Need help with empty arrays
Quote:

Originally Posted by Norm
What is the value of the variable: elements for the array you just posted?
Consider: X*0 = 0 & (0 == 0) is true

Aha yes sorry. It is 0! I thought it would be 3*1=3 (3 rows, 1 column). Why is that?

Now I see it's obvious why it returns the value true, since counter==elements is true...
• March 23rd, 2013, 09:43 AM
Norm
Re: Need help with empty arrays
You need to look at each row (in a loop) to check if any of the rows have elements.
• March 23rd, 2013, 09:52 AM
robin_
Re: Need help with empty arrays
Quote:

Originally Posted by Norm
You need to look at each row (in a loop) to check if any of the rows have elements.

Well isn't that what I am doing? I'm looking if there is any integer in the matrix, if so. I count +1. And the elements matches the dimension of the matrix. That's why I compare elements with count, since if there is an integer at every spot, the variable count will be equal to elements. But now the elements==count doesn't work very well...

However, do you mean like this? But I loop through the variable i? Because this works!

if(matrix.length[i]==0){
return false;
}

• March 23rd, 2013, 10:00 AM
Norm
Re: Need help with empty arrays
What is matrix.length[i] ?
• March 23rd, 2013, 10:16 AM
robin_
Re: Need help with empty arrays
Quote:

Originally Posted by Norm
What is matrix.length[i] ?

Sorry I mean matrix[i].length of course.
• March 23rd, 2013, 10:26 AM
Norm
Re: Need help with empty arrays
I don't "of course" any code that an OP posts. There are so many different ways to write code that I don't try to guess the problem after seeing 3 lines of code doesn't show what the rest of the code has in it.
• March 23rd, 2013, 10:46 AM
robin_
Re: Need help with empty arrays
Yeah you are right, I'm sorry. However I'm still thankful for your help because you made me find the problem when you asked about the variable -elements. I never had the thought of matrix[i].length to be equal to 0 for this particular array. I thought it was going to be 1.

I have a question regarding this problem. Why isn't it possible to write an if-statement like if(matrix[i][j]==null)??? I can also add that my knowledge about the functionality of "null" in JAVA is 0.

Thanks.
• March 23rd, 2013, 10:53 AM
Norm
Re: Need help with empty arrays
The validity of the condition: matrix[i][j]==null would depend on the data type of the element in the array.
object references can hold a null value
primitive data type elements can not.
For example int x = null; is not valid

However with multidimensional arrays, missing dimensional parts can be tested
Code :

``` int[][][] mat= {{{}}}; System.out.println(mat[0][0] == null); // this works System.out.println(mat[0][0][0] == null); // this does not work```
• March 23rd, 2013, 11:00 AM
robin_
Re: Need help with empty arrays
Quote:

Originally Posted by Norm
The validity of the condition: matrix[i][j]==null would depend on the data type of the element in the array.
object references can hold a null value
primitive data type elements can not.
For example int x = null; is not valid

However with multidimensional arrays, missing dimensional parts can be tested
Code :

``` int[][][] mat= {{{}}}; System.out.println(mat[0][0] == null); // this works System.out.println(mat[0][0][0] == null); // this does not work```

Oh, thats why... thanks Norm!