Let's take arrays out of the equation - an array is just a collection of variables, really. Let's consider a single variable. Suppose we had a method like this:
public boolean isEmpty(char c)
What would that do? The value of c cannot be null, because char is a primitive type. It could be U+0000 (aka '\u0000' or '\0' as character literals in Java), and that's the default value of char (for array elements and fields) but it's not the same as a null reference.
If you want a type which is like char but is a reference type, you should consider using Character - the wrapper type for char just like Integer is for int. Or if you know that you'll never use U+0000 as a valid value, you could just stick with that.
However, a better alternative would often be to design your code so that you don't need the concept of "empty or not empty". We don't know what you're trying to achieve here, but it's usually a good thing to at least consider. For arrays, the alternative is often to use an ArrayList<E> - but of course you can't have an ArrayList<char> in Java as Java generics don't allow primitive type arguments :(
Let's take arrays out of the equation - an array is just a collection of variables, really. Let's consider a single variable. Suppose we had a method like this:
public boolean isEmpty(char c)
What would that do? The value of c cannot be null, because char is a primitive type. It could be U+0000 (aka '\u0000' or '\0' as character literals in Java), and that's the default value of char (for array elements and fields) but it's not the same as a null reference.
If you want a type which is like char but is a reference type, you should consider using Character - the wrapper type for char just like Integer is for int. Or if you know that you'll never use U+0000 as a valid value, you could just stick with that.
However, a better alternative would often be to design your code so that you don't need the concept of "empty or not empty". We don't know what you're trying to achieve here, but it's usually a good thing to at least consider. For arrays, the alternative is often to use an ArrayList<E> - but of course you can't have an ArrayList<char> in Java as Java generics don't allow primitive type arguments :(
An element of a primitive array can't be null. It will always have a default value if you didn't initialize it yourself. The default for char is 0.
Android JAVA how to check if some index of char array is empty - Stack Overflow
How to check if a char array is blank (or at least 20 ?)<< SOLVED>>
java - Checking if char is empty - Stack Overflow
java - How to check if a char is equal to an empty space? - Stack Overflow
Change it to: if(position[i][j] == 0)
Each char can be compared with an int.
The default value is '\u0000' i.e. 0 for a char array element.
And that's exactly what you meant by empty cell, I assume.
To test this you can run this.
class Test {
public static void main(String[] args) {
char[][] x = new char[3][3];
for (int i=0; i<3; i++){
for (int j=0; j<3; j++){
if (x[i][j] == 0){
System.out.println("This char is zero.");
}
}
}
}
}
Assuming you have initialized your array like
char[] position = new char[length];
the default value for each char element is '\u0000' (the null character) which is also equal to 0. So you can check this instead:
if (postision[i][j] == '\u0000')
or use this if you want to improve readability:
if (positionv[i][j] == 0)
In C++, objects of scalar types are not zero-initialized. They are default-initialized, which means the value they get at initialization is undefined.
In particular, the value is unlikely to be 0 for chars, int, etc., and definitely you shouldn't rely on it to have any particular value. If you want your array cells to be initialized to 0 before you start working with them, you have to initialize them manually.
If you come from a Java world, you might think this is an unnecessary bulk of work, but consider that you're working with C-style arrays, and C is not meant to sacrifice performance for programmer time. There are cases where an initialization to 0 would uselessly waste CPU time, and you don't want to pay for what you don't use.
There is no way to check if a variable has been "set" or not. You can only check if it is equal to a specific value.
Your code in java seems to work because, in Java, all primitive data types that are created but not initialized are given a default value by the compiler. In the case of a char, it's '\u0000', which is equivalent to 0.
In c++ the values of these characters are undefined. If you want to have the same behavior, you'll need to explicitly set all of the characters to 0 before you do your check.
if (c == ' ')
char is a primitive data type, so it can be compared with ==.
Also, by using double quotes you create String constant (" "), while with single quotes it's a char constant (' ').
The code you needs depends on what you mean by "an empty space".
If you mean the ASCII / Latin-1 / Unicode space character (0x20) aka SP, then:
if (ch == ' ') { // ... }If you mean any of the traditional ASCII whitespace characters (SP, HT, VT, CR, NL), then:
if (ch == ' ' || ch == '\t' || ch == '\r' || ch == '\n' || ch == '\x0b') { // ... }If you mean any Unicode whitespace character, then:
if (Character.isWhitespace(ch)) { // ... }
Note that there are Unicode whitespace includes additional ASCII control codes, and some other Unicode characters in higher code planes; see the javadoc for Character.isWhitespace(char).
What you wrote was this:
if (Equals(ch, " ")) {
// ...
}
This is wrong on a number of levels. Firstly, the way that the Java compiler tries to interpret that is as a call to a method with a signature of boolean Equals(char, String).
- This is wrong because no method exists, as the compiler reported in the error message.
Equalswouldn't normally be the name of a method anyway. The Java convention is that method names start with a lower case letter.- Your code (as written) was trying to compare a character and a String, but
charandStringare not comparable and cannot be cast to a common base type.
There is such a thing as a Comparator in Java, but it is an interface not a method, and it is declared like this:
public interface Comparator<T> {
public int compare(T v1, T v2);
}
In other words, the method name is compare (not Equals), it returns an integer (not a boolean), and it compares two values that can be promoted to the type given by the type parameter.
Someone (in a deleted Answer!) said they tried this:
if (c == " ")
That fails for two reasons:
" "is a String literal and not a character literal, and Java does not allow direct comparison ofStringandcharvalues.You should NEVER compare Strings or String literals using
==. The==operator on a reference type compares object identity, not object value. In the case ofStringit is common to have different objects with different identity and the same value. An==test will often give the wrong answer ... from the perspective of what you are trying to do here.
Given this code:
char text[50];
if(strlen(text) == 0) {}
Followed by a question about this code:
memset(text, 0, sizeof(text));
if(strlen(text) == 0) {}
I smell confusion. Specifically, in this case:
char text[50];
if(strlen(text) == 0) {}
... the contents of text[] will be uninitialized and undefined. Thus, strlen(text) will return an undefined result.
The easiest/fastest way to ensure that a C string is initialized to the empty string is to simply set the first byte to 0.
char text[50];
text[0] = 0;
From then, both strlen(text) and the very-fast-but-not-as-straightforward (text[0] == 0) tests will both detect the empty string.
Depends on whether or not your array is holding a null-terminated string. If so, then
if(text[0] == '\0') {}
should be sufficient.
Edit: Another method would be...
if (strcmp(text, "") == 0)
which is potentially less efficient but clearly expresses your intent.