Programming in Java: Arrays and Array Lists. Last delivered in 2013. All educational material listed or linked to on these pages in relation to King's College London may be provided for reference only, and therefore does not necessarily reflect the current course content.
13. Why?
fish
product1
steak
product2
lamb
product3
How can I tell what is in my basket
without directly referencing each
variable name?
I can’t (in a reasonable amount of code).
For a basket of 200 products, calculating
the total bill would take lots of code.
18. Why?
fish steak lamb
products
0 1 2
I can now write a short piece of code
which looks at the first item in the list, then
the second, third etc. until the end, and
calculates the total bill. In this process only
one variable is referenced.
This is an array.
22. •When we declare a new array, we have to give it a
fixed size.
•This means that, aside from manually expanding the
size of the array in memory, we cannot have a list
which holds an arbitrary number of items.
•To address this issue, we introduce array lists.
•Array lists perform exactly the same
operations as an array, but their syntax is
different.
26. SUMMARY
• Arrays allow us to store lists of related items so that we can
access them more efficiently.
• As Arrays are of a fixed size, we also rely on ArrayLists, the
size of which can expand as necessary.
• Although the syntax of an ArrayList is different, we can still
carry out the same operations.
27. SUMMARY
• This is the very bare bones of Arrays and ArrayLists.
• For everything you need to know, you should consult the
lecture notes, SGTs and labs.