-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;

}

1

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.