Java: resize method, can someone explain?

Viewed 1521

I am not sure how this method works. What I understand is that we create an temporary array with double the size of the theItems.length (theItems is another array). After, we copy the items to the temp array and in the end we write theItems = temp; (I am not sure why and what happens)(does it mean that theItems double its size too?). Can't we double the size of theItems without using temp?

private void resize() {
    String[] temp = new String[theItems.length*2];
    for (int i=0 ; i < noOfItems ; i++){
        temp[i]=theItems[i];
    }
    theItems=temp;
}
3 Answers

I am not sure why and what happens

You are creating another array with more room for additional elements. Arrays have a fixed size in Java; once created, it cannot be changed. Here, the new array's length is double the old one. Then a simple for loop copies element references.

does it mean that theItems double its size too?

No, the array reference theItems is reassigned to the new, bigger array just created.

Can't we double the size of theItems without using temp?

You could just replace theItems with a new array, but then you just lost the reference to the original array that has all the items you want to retain, so that's not useful.

Here's what happens:

  1. Initial condition.

    theItems -> ["one", "two", "three"]
    
  2. New array created.

    theItems -> ["one", "two", "three"]
    
    temp     -> [null , null , null , null, null, null]
    
  3. Items copied.

    theItems -> ["one", "two", "three"]
    
    temp     -> ["one", "two", "three", null, null, null]
    
  4. The variable theItems is reassigned.

    theItems \       ["one", "two", "three"]  <- will be garbage collected.
             |
    temp   --+> ["one", "two", "three", null, null, null]
    

The variable temp will go out of scope, but theItems will still refer to the new array.

String[] temp = new String[theItems.length*2]; creates an Array twice the size of theItems. So say if theItems was {"Hello", "Hi", "Goodbye", "Bye"} we would then have

__ __ __ __ __ __ __ __

as temp. Then

for(int i=0; i<noOfItems; i++){
    temp[i]=theItems[i];
}

Copies the items onto temp. So we would have:

"Hello", "Hi", "Goodbye", "Bye" __ __ __ __

(Four empty spaces at the end)

And then theItems=temp; assigns theItems to temp. This way the variable theItems will have four empty spaces, and more items can be added to it.

Note that the Arrays class already has a function built in to do this: Arrays.copyOf

To answer your question at the end, you can use helper methods like the comment demonstrates, but somewhere, something must create a separate new array and copy all the items over.

Arrays are a fixed-sized construct--once you create one, you can't make it larger or smaller in-place. When you create an array, a section of Java's heap (which is a term for the area of memory where objects are stored) equal to the size of one item (in this case, a reference to a String) times the length of the array is reserved, and a reference to that memory is assigned to the variable you specify on the left hand side of the statement, as in String[] items = new String[10].

So to make items larger, you have to create a new array at the size you need, copy all the existing items over in order, and then reassign the variable that held the smaller array to the reference of the larger array, at that point held in temp. items = temp; simply copies the reference into items so items and temp refer to the same array. Then when the method ends, items remains while temp is removed.

Additionally, since there is now an array (which items previously referred to) that no variable refers to now, Java will take care of cleaning up that old array during its garbage collecting cycles. You don't have to worry about the items in your array going away, however, because you copied their references into the new array.

Related