-4

i have question about this line(theData = theData1;) what is mean? i know we create anothor array called theData1 and copy the elements so the line what does it mean?

 private void reallocate () 
 {
capacity *= 2; 
      E[] theData1 = (E[]) new Object[capacity];
      for (int i = 0; i < size; i++)
             theData1[i] = theData[i];
theData = theData1;

}

banoosh
  • 1
  • 2
  • Related: [Array assignment and reference in Java](https://stackoverflow.com/questions/44732712/array-assignment-and-reference-in-java). You may want to search for more. – Ole V.V. Jul 08 '19 at 15:58

1 Answers1

0

Your method virtually extends the array theData to double capacity. Since a Java array cannot grow, the way to obtain an array double as large is to create a new array, theData1, move all the elements over and then keep the new array instead of the old, smaller one. The line you are asking about is:

    theData = theData1;

This assigns the reference to the new array to the variable holding the array (formerly the old array). So from this point theData refers to the new array. Thereby the extension operation is complete.

Arrays in Java are reference types (as opposed to primitive types). You can search for much more about what it means.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161