Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Programming in Java: Arrays and Array Lists

116 views

Published on

Programming in Java: Arrays and Array Lists

Published in: Education
  • Be the first to comment

  • Be the first to like this

Programming in Java: Arrays and Array Lists

  1. 1. ARRAYS AND ARRAY LISTS Martin Chapman martin.chapman@kcl.ac.uk
  2. 2. What? 1 56 goat dog 34 cat
  3. 3. What? 1 56 goat dog 34 cat number1 number2 number3 string1 string2 string3
  4. 4. 34 number3 What? 1 56 goat dog cat number1 number2 string1 string2 string3
  5. 5. 34 number3 What? 1 56 goat dog cat number1 number2 string1 string2 string3
  6. 6. 34 What? 1 56 goat dog cat numbers strings
  7. 7. 34 What? 1 56 goat dog cat numbers strings 0 1 2 0 1 2
  8. 8. Why?
  9. 9. Why? fish product1 Make a new variable called product1. Add fish information to it.
  10. 10. Why? fish product1 steak product2
  11. 11. Why? fish product1 steak product2 lamb product3
  12. 12. Why? fish product1 steak product2 lamb product3 How can I tell what is in my basket without directly referencing each variable name?
  13. 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.
  14. 14. Why? products 0 1 2
  15. 15. Why? fish products 0 1 2
  16. 16. Why? fish steak products 0 1 2
  17. 17. Why? fish steak lamb products 0 1 2
  18. 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. 19. How? products 0 1 2 data type 3 ‘slots’ in memory
  20. 20. How? products 0 1 2 fish steak lamb name[index]
  21. 21. How? i is the index to the array array length
  22. 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. 23. How? data type 3 ‘slots’ in memory class object
  24. 24. How? name[index] object.method() 0 1 2
  25. 25. How? object.get(index) object.size()
  26. 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. 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.

×