go to previous page   go to home page   go to next page

Will the following statement make the array larger?

list = new String[5];

Answer:

No. The statement creates a completely new array object (with 5 cells). The old array object and the information it contains is now garbage unless some other reference variable points to it.


Old Information is Lost

old array and new array

 

In the example, the reference variable list is set to the newly constructed object. The diagram shows what happens. The diagonal slashes represent null, the value that means "no object".

The information in the old object is not automatically transferred to the new object. To keep the old information, the program must keep a reference to the old array and copy the old information to the new array. This is not too hard to do, but it is a nuisance.


QUESTION 3:

What happens if information is added to a cell that does not exist?

String[] list = new String[3];
list[0] = "ant" ;
list[1] = "bat" ;
list[2] = "cat" ;

list[3] = "dog" ;