Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Need help with empty arrays

1. ## 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

`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,
```int elements = matrix.length*matrix.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).
```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  Reply With Quote

3. ## Re: Need help with empty arrays

What is the value of elements for the array you posted?  Reply With Quote

4. ## Re: Need help with empty arrays 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.  Reply With Quote

5. ## 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  Reply With Quote

6. ## Re: Need help with empty arrays 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...  Reply With Quote

7. ## 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.  Reply With Quote

8. ## The Following User Says Thank You to Norm For This Useful Post:

robin_ (March 23rd, 2013)

9. ## Re: Need help with empty arrays 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;
}  Reply With Quote

10. ## Re: Need help with empty arrays

What is matrix.length[i] ?  Reply With Quote

11. ## Re: Need help with empty arrays Originally Posted by Norm What is matrix.length[i] ?
Sorry I mean matrix[i].length of course.  Reply With Quote

12. ## 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.  Reply With Quote

13. ## 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.  Reply With Quote

14. ## 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
```   int[][][] mat= {{{}}};
System.out.println(mat == null);       //  this works
System.out.println(mat == null);  // this does not work```  Reply With Quote

15. ## The Following User Says Thank You to Norm For This Useful Post:

robin_ (March 23rd, 2013)

16. ## Re: Need help with empty arrays 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
```   int[][][] mat= {{{}}};
System.out.println(mat == null);       //  this works
System.out.println(mat == null);  // this does not work```
Oh, thats why... thanks Norm!  Reply With Quote 