How to check a null position in the vector

Asked

Viewed 9,251 times

2

Why do if(array[i] == null) is not correct?

What would be the best way to check if that vector space is "empty"?

  • Where did you see that comparison not correct? If it’s inside one, it might not be the best way to do it, but it wouldn’t be wrong.

  • What is the type of the array? To me, unless the base type of the array is a primitive type, this is correct yes.

  • the vector is of type int

2 answers

3


There is a fundamental difference between a null matrix and an empty matrix . This is a test for null .

int arr[] = null;
if (arr == null) {
  System.out.println("array is null");
}

For you check matrix is empty use;

arr = new int[0];
if (arr.length == 0) {
 System.out.println("array is empty");
}

An alternative definition of " empty " is , if all elements are null :

Object arr[] = new Object[10];
boolean empty = true;
for (int i=0; i<arr.length; i++) {
  if (arr[i] != null) {
    empty = false;
    break;
  }
}

Or;

Object arr[] = new Object[10];
boolean empty = true;
for (Object ob : arr) {
  if (ob != null) {
    empty = false;
    break;
  }
}  

Elements in primitive matrices cannot be empty. They are always initialized for something ( usually 0 for int matrices , but it depends on how you declare the matrix ) .

If you declare the matrix like this, for example:

Reference Here.

int [] myArray ;
myArray = new int[7] ;

All elements will be the default for 0 . An alternative syntax for declaring matrices is

int[] myArray = { 12, 7, 32, 15, 113, 0, 7 };

Where the initial values for a matrix ( of size seven in this case ) are given in the keys { }.

Reference Here.

  • 1

    Your answer is much better now. + 1.

2

Considering what you said in this comment:

the vector is of type int - Dr.G

The problem is this:

The type of the array is int[]. Which means that array[i] is the type int. The problem is to compare int with null. Primitive types will never be null and the compiler knows it. So it will give you a build error:

incomparable types: int and <null>
  • but like, let’s assume that I have an integer vector v[], then I added 3 at position 0, now I want to delete the content from position 0, in my view I should say that v[0] = null, but I think this is wrong. The intention is to try to simulate Arraylist functions like add and remove with common arrays.

  • @Dr.G There is no way to delete positions in an array. In an array whose base type is an object, you can set a position like null, but the position continues to exist. In the case of a int[], it already comes automatically filled with a lot of zeros. It is impossible in Java to put up null within a int[]. In addition, an array has a fixed size, regardless of how many or which elements have as content null. I think what you need is a java.util.List and not an array.

  • 1

    @Dr.G Internally, the ArrayList maintains a counter of how many positions in the array have been used, the internal array can be (and often is) much larger than that counter. The methods of the ArrayList do not allow positions beyond the size limit of the ArrayList (and not the array) are accessed, even if they exist in the array. When the internal array is too small, it creates a new array twice the size, copies the elements and discards the old array. To remove elements, it copies all subsequent elements a rearward position and decreases the counter by placing null only in the end.

  • That’s what I wanted to know

Browser other questions tagged

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