What are the consequences of such actions if the

This preview shows page 97 - 101 out of 101 pages.

What are the consequences of such actions? If the array is too large this wastes a lot memory. When the array is full the program may be forced to stop.
Image of page 97

Subscribe to view the full document.

Solution 2: variable size arrays Create an array of a reasonable default capacity. Increase or decrease its size according to the need. This means that the logical size of the array will not correspond to its physical size . Which means that it’s up to the programmer to maintain information about the logical size (the instance variable length of an array refers to its physical size). It’s the responsibility of the programmer to access elements that are below the logical size.
Image of page 98
Qualify the behavior of the solution as the size of the array increases. All the elements of the array have to be copied. The more elements there are in the array the more copies are needed. Initially, there are only few elements to be copied, but the larger the array the more copies are needed. Insertion and resize are related to one another. Once the logical size of the array equals its physical size every subsequent insertion necessitates resizing the array, which has a cost proportional to the number of elements in the array.
Image of page 99

Subscribe to view the full document.

A more practical solution consists of doubling the size of the array whenever the logical size of the array equals its physical size. What have achieved? Not all insertions require resizing the array, hence copying its elements. What has been lost? Memory efficiency. for some applications, the logical size of the array can also decrease, in which cases, the physical size of the array could also be decreased whenever the number of elements is below a certain threshold.
Image of page 100
1. is the array big enough? 2. where do we start coping the elements? implement remove(int pos)
Image of page 101
You've reached the end of this preview.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern