1 / 18

Array Lists

Array Lists . Chapter 7.2 Pages 248 - 253. Array Lists. In Java, Arrays are an important structure for storing data. We will learn more about arrays later, but for now, you need to know: An array is a collection of similar objects that are stored sequentially in the same block of memory.

steven-wall
Download Presentation

Array Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Array Lists Chapter 7.2 Pages 248 - 253

  2. Array Lists • In Java, Arrays are an important structure for storing data. • We will learn more about arrays later, but for now, you need to know: • An array is a collection of similar objects that are stored sequentially in the same block of memory. Object1 Object2 Object3 Object4

  3. Array Lists • An Array’s items arestored sequentially in a block memory (RAM). • RAM stands for Random Access Memory • Random Access means that any random (arbitrary) block of memory can be accessed simply specifying the memory address. • In English, this means you do not have to scan RAM sequentially to find stuff, like we did with files.

  4. Array Lists • Arrays and RAM work to allow you to access (“jump”) to almost any memory location. • By placing objects together in memory, you can jump directly to a specific object. • If you know • The start of the array and • the size of the object, • you could jump to the • first object, • fifth object, or • nth object.

  5. File vs. RAM File RAM 0 8 30 2010 10009.73 16 8 27 2010 10150.65 32 8 26 2010 9985.81 48 8 25 2010 10060.06 … 4816 8 20 2010 10213.62 8 30 2010 10009.73 8 27 2010 10150.65 8 26 2010 9985.81 8 25 2010 10060.06 8 24 2010 10040.45 8 23 2010 10174.41 8 20 2010 10213.62

  6. Lists • We all know what a list is, right? • A List is a series of items that are ordered. • In Java, a List is an interface. • An interface describes a set of actions on a data structure

  7. List Actions (methods) • add • An item to the list • remove • An item specified by its value • An item specified by its index (order number) • contains • Does the list contain an item? • size of the list • get • Item at index • indexOf • The item you are searching for

  8. Array Lists • An ArrayList is class that implements the List interface using Arrays as the underlying data structure. • Lists could be implemented with files or pointers • Arrays are great way to implement lists

  9. ArrayLists • ArrayListclass manages a sequence of objects • Can grow and shrink as needed • ArrayListclass supplies methods for many common tasks, such as inserting and removing elements • ArrayListis a generic class: • ArrayList<T> • collects objects of type parameterT: • ArrayList<String> names = new ArrayList<String>(); • names.add("Emily"); • names.add("Bob"); • names.add("Cindy"); • sizemethod yields number of elements

  10. AddingElements • To add an object to the end of the array list, use the addmethod: • names.add("Emily"); • names.add("Bob"); • names.add("Cindy");

  11. Retrieving Array List Elements • To obtain the value an element at an index, use the getmethod • Index starts at 0 • String name = names.get(2); • // gets the third element of the array list • Bounds error if index is out of range • Most common bounds error: • int i = names.size(); • name = names.get(i); // Error • // legal index values are 0 ... i-1

  12. SettingElements • To set an element to a new value, use the setmethod: • names.set(2, "Carolyn");

  13. RemovingElements • To remove an element at an index, use the remove method: • names.remove(1);

  14. Contains • To remove an element at an index, use the remove method: • if (names.contains(“Carolyn”)) • System.out.println(“Carolynis here”);

  15. Adding and Removing Elements • names.add("Emily"); • names.add("Bob"); • names.add("Cindy"); • names.set(2, "Carolyn"); • names.add(1, "Ann"); • names.remove(1);

  16. Working with Array Lists

  17. Working with Array Lists (cont.)

  18. Syntax 7.2 ArrayLists

More Related