0

My current class project is dealing with hashtables. We are able to use whatever language we want, however the professor uses c++.

This is the sample code he provided us that we are supposed to copy over:

 for(int i = 0; i < size; i++)
      this->theTable[i] = EMPTY;

He says this will set the array, theTable, to empty.

How am I supposed to do this in Java? Isn't an array in Java always set to 0 if its empty? Is this code equivalent?

 for(int i = 0; i < size; i++) {
           this.theTable[i] = 0;
 }
7
  • What type is theTable? Commented Nov 4, 2018 at 23:45
  • That code is not necessary in Java, as you noted every Java array has a default initial value for its' elements. int[] theTable = new int[size]; is equivalent. Assuming theTable is an array of int. Commented Nov 4, 2018 at 23:46
  • Yes, theTable is an int array, sorry. So no point to have the for loop then correct? Commented Nov 4, 2018 at 23:48
  • In addition to what @ElliottFrisch said, this code is correct (and needed) if theTable is of type Integer[] Commented Nov 4, 2018 at 23:49
  • What is EMPTY? Commented Nov 4, 2018 at 23:50

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.