180 likes | 274 Vues
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.
E N D
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. Object1 Object2 Object3 Object4
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.
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.
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
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
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
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
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
AddingElements • To add an object to the end of the array list, use the addmethod: • names.add("Emily"); • names.add("Bob"); • names.add("Cindy");
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
SettingElements • To set an element to a new value, use the setmethod: • names.set(2, "Carolyn");
RemovingElements • To remove an element at an index, use the remove method: • names.remove(1);
Contains • To remove an element at an index, use the remove method: • if (names.contains(“Carolyn”)) • System.out.println(“Carolynis here”);
Adding and Removing Elements • names.add("Emily"); • names.add("Bob"); • names.add("Cindy"); • names.set(2, "Carolyn"); • names.add(1, "Ann"); • names.remove(1);