Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (anonymous):

How do I check whether an array is empty or full?

OpenStudy (anonymous):

Current code: public boolean isEmpty() { if (top = 0) empty = true; } public boolean isFull() { if (stackArray.length == 100) full = true; } It says that it's expecting a boolean or a return statement and I'm not sure how to make it work.

OpenStudy (anonymous):

bool isEmpty() { if (stackArray.length == 0) return true; } bool isFull() { if (stackArray.length == 100) return true; }

OpenStudy (anonymous):

bool arrayHasMembers(array) { if(array.length==0) return false; else : true; } One function to rule them all... or just the array, and cleaner and less work.

OpenStudy (anonymous):

@JuanV but ur function can't tell that array is completely fill or not

OpenStudy (anonymous):

You could then have if(array == 0){ empty = true; full = false; return;} Else if(array == 100){ empty = false; full = true; return;} else: {empty = false full = false;} You are almost never going to test array for a Full array spec - Because it should be tracking the array while it is in memory for its members size, in case it needs to be widened for more. So you are really going to test can this array fit X amount of new member. If not widen array. Maybe you want to know if it has all the members of a table, and you know how many members are in that table, but that is not good programming. 0 should always be the test case, just to see if it not empty or of null type. other then that it should be array.length + newMembers <= array.size

OpenStudy (rsmith6559):

The way that your code is written, you only return a boolean if the if statement is true. If the if statement is false, you don't return anything. Here's a way to consider: public boolean isFull() { return( ( stackArray.length == ( top + 1 ) ) ? true : false; } I like the ternary operator.

OpenStudy (rsmith6559):

Darn, I forgot the last paren again! return( ( stackArray.length == ( top + 1 ) ) ? true : false );

OpenStudy (anonymous):

public boolean isFull() { return stackArray.length == ( top + 1 ); } A conditional will always evaluate to true or false. You only need to return the conditional.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!