user2920249
user2920249

Reputation: 137

Incompatible types: int and <nulltype>

I am having trouble with the (hashTable[bucket][size] == null). I was wondering how you could check to see that slot is null if null is not of type int. Any ideas/tips?

public void add(int n){
   int bucket = hashF(n);
   int p=0;
   int size = hashTable[0].length;
  for(int i=0; i>size; i++){
    if(hashTable[bucket][size] == null){
      hashTable[bucket][size]= n;
      break;
    }
    if(i+1 == size){
      bucket++;
      i = -1;
    }
    if(bucket ==10){
      bucket = 0;
    }
    p++;
    if(p== hashTable.length*hashTable[0].length){
      break;
    }
  }
  for(int i=0; i<BST.length; i++){
    if(BST[i] == null){
      BST[i] = n;
      break;
    }
    else if(n<BST[i]){
      i= 2*i;
    }
    else{
        i = 2*i+1;
      }
  }
 }

Upvotes: 2

Views: 7805

Answers (2)

Bohemian
Bohemian

Reputation: 425278

Java primitives (like int) have no "unassigned" value - they always have a value.

If you want to represent "unassigned" use an "impossible" value for the size, such as -1 which you would assign at initialization. Then in code test for hashTable[bucket][size] == -1 to determine if it's in it's initial state.

Upvotes: 0

JamesB
JamesB

Reputation: 7894

Assuming that hashTable is a 2D array of ints, you cannot check if one of its elements will be null as it never will be. A primitive can never be null as it is not an object reference. It will always have a value - its default value is 0.

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Upvotes: 1

Related Questions