How to check if a field of an array of chars is empty in Java?

Asked

Viewed 611 times

-2

I am trying to check whether a particular position of an array of chars actually contains some character.

I tried it two ways:

char[] arr = new char[10];

if(arr[0] == null){

}

And also:

char[] arr = new char[10];
if(arr[0] == ''){

}

When typing any of the above versions of the code, Netbeans points out error in comparison but does not suggest correction.

How can I make this check?

  • 1

    You need to define what is empty first. The two cases do not exist, one is asking if there is a null reference, which is impossible since one char is not a reference. The second is checking if it has 0 Charas, but it is certain that it has 1 char there, you have reserved space for it, then there is no way not to have. So what is empty? These two conditions are impossible.

  • 3

    Check arr[i] == 0 you check if that position is being used, but you need to define better even what you want as the Maniero said.

  • 2

    You did char[] arr = new char[10]; then your array has 10 characters and each position starts with the character 0 ('\0') not to be confused with the character '0'. And you always have to have a character in every position.

  • I changed the question... I want to know if it contains any characters in that position or if nothing has been inserted yet.

  • 3

    As already spoken 3 times, it contains a character obligatory. You must then determine what you want. If you just want to know if it contains any characters, you don’t have to do anything, you already know it contains them. Now if you want to know if you have certain specific characters you need to define this.

  • @Isac, if you post your comment as reply I register as solved...

  • 2

    This here is a XY problem. Why do you want "check whether a given position of an array of chars actually contains some character"? Where are you going with this?

  • 3

    In an array of chars, all the positions at all times has a character. This happens with any array of primitive types. So, why do you want to do this?

  • Thank you for clarifying that Victor. I didn’t know. I thought that every space in the array would be filled with "nothing". I decided to check if the position had the character I seek. (in this array would only have that character that interests me, what you have in the rest doesn’t matter).

Show 4 more comments

1 answer

1


I believe there is no "empty" representation for the type char which is primitive, like '', then you have to look at it differently. I looked at the class documentation Character and I found the MIN_VALUE which is a constant for the value '\u0000' which in this case is 0 (zero).

Take an example:

import java.lang.*;

class Main 
{
  public static void main(String[] args) 
  {
    char[] arr = new char[] {'a','b', Character.MIN_VALUE};
    if (arr[1] == Character.MIN_VALUE)
    {
      System.out.println("vazio");
    }    
    else 
    {
      System.out.println("nao vazio");
    }
  }
}

Exit:

nonempty

Therefore, to fix better see this table with default values for all primitive types:

valores padrão

Table source

If you want "empty" values in the array you can use this constant. There is this question also. Always seeks to prioritize documentation in your searches, if there is not there you can ask here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.