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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Need help with empty arrays

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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[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).
    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
    Last edited by robin_; March 23rd, 2013 at 07:10 AM. Reason: fixing my indexing...


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Need help with empty arrays

    What is the value of elements for the array you posted?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help with empty arrays

    Quote Originally Posted by Norm View Post
    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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default 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
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help with empty arrays

    Quote Originally Posted by Norm View Post
    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...

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

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

    robin_ (March 23rd, 2013)

  8. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help with empty arrays

    Quote Originally Posted by Norm View Post
    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;
    }

    Thanks for your help!

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default Re: Need help with empty arrays

    What is matrix.length[i] ?
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help with empty arrays

    Quote Originally Posted by Norm View Post
    What is matrix.length[i] ?
    Sorry I mean matrix[i].length of course.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default 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.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,094
    Thanks
    48
    Thanked 2,152 Times in 2,125 Posts

    Default 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[0][0] == null);       //  this works
       System.out.println(mat[0][0][0] == null);  // this does not work
    If you don't understand my answer, don't ignore it, ask a question.

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

    robin_ (March 23rd, 2013)

  15. #13
    Junior Member
    Join Date
    Mar 2013
    Posts
    15
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Need help with empty arrays

    Quote Originally Posted by Norm View Post
    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[0][0] == null);       //  this works
       System.out.println(mat[0][0][0] == null);  // this does not work
    Oh, thats why... thanks Norm!

Similar Threads

  1. Why am I getting an empty window here
    By zlloyd1 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 17th, 2012, 07:33 AM
  2. My spinner is empty
    By VagosM in forum Android Development
    Replies: 0
    Last Post: November 28th, 2012, 04:28 PM
  3. Empty If Statement
    By lightOfDay in forum Loops & Control Statements
    Replies: 5
    Last Post: June 14th, 2012, 08:41 AM
  4. Check if 'int' is empty
    By TrYk in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 10th, 2012, 07:34 AM
  5. extended JPanel is empty
    By pottsiex5 in forum AWT / Java Swing
    Replies: 4
    Last Post: November 26th, 2011, 02:59 PM

Tags for this Thread