Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How can i check if an Object[][] is empty  RSS feed

 
Krit Christoforou
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This might be a stupid question with a really easy answer...but How can i check if my Object[][] contains anything?
I've been looking at java api's for over an hour and i cant figure a way to do that..
thanks
 
Hentay Duke
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not sure but if you check for it's length and it returns null, that might give you what you need.
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first thing you need to do is give a precise definition what you mean by "empty". I can think of several possibilities:

1) The array reference is null. In this case you just check for a null reference:


2) The array has length 0. The check is quite similar to the above:


3) The subarrays are all null or length 0. In this case you have to check each subarray similar to 1 or 2 above.

4) The individual elements in the array are set to null. In this case, you need to check each individual element.

So what do you mean by "empty"?

Layne
 
Ryan McGuire
Ranch Hand
Posts: 1142
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[I see that Layne posted an answer while I was composing mine, but I think I'll go ahead anyhow. It gives me a warm fuzzy that we had basically the same idea.]

There are any number of levels of "empty".

Something declared as Object[][] is a reference to an array object that holds M references to array objects that each hold N references to Objects.

If you have Object[][] x,
  • x could be null
  • x could have a zero length
  • any or all of the rows of x could be null
  • any row of x could be non-null but have a zero length
  • any or all given column in a row could be null
  • or, finally, a given row/column of x could reference an Object.


  • Here try this:



    Ryan
    [ May 03, 2005: Message edited by: Ryan McGuire ]
     
    Krit Christoforou
    Ranch Hand
    Posts: 46
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Right.
    I wrote this piece of code:


    Basically i *only* want to create the table, if the Object[][] i get from pd.toObject() is not empty/null.
    I can't get it to work, i have already tried your suggestions...As for .length() i get a compiler error.
    I also tried this:
    if (((Object[][]) pd.toObject())!=null) {....
    and it doesnt work.
    Any further ideas?
    Correct me if i'm wrong.
    Thanks for your help
     
    Shyam Prasad Murarka
    Ranch Hand
    Posts: 209
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    In your if condition change the condition to:
    if(pd.toObject()!= null && pd.length!=0)

    Note: To find the length of an array use its variable called length. What you did was length(). Don't use it as a function.
     
    Krit Christoforou
    Ranch Hand
    Posts: 46
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Works like a charm
    Thanks Shyam!
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!