ArrayLists and Iterators
Read this chapter about lists and the useful methods that are available for adding and inserting elements into a list.
3. Old Information is Lost
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
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 array is not transferred to the new array. 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" ;