SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
12.
Why?
fish
product1
steak
product2
lamb
product3
How can I tell what is in my basket
without directly referencing each
variable name?
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.
19.
How?
products
0 1 2
data type
3 ‘slots’ in
memory
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.
23.
How?
data type
3 ‘slots’ in
memory
class object
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.