Presentation is loading. Please wait.

Presentation is loading. Please wait.

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,

Similar presentations


Presentation on theme: "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,"— 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 are stored 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  8 30 201010009.73 8 27 201010150.65 8 26 20109985.81 8 25 201010060.06 8 24 201010040.45 8 23 201010174.41 8 20 201010213.62 RAM 0  8 30 201010009.73 16  8 27 201010150.65 32  8 26 20109985.81 48  8 25 201010060.06 … 4816  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 Array Lists ArrayList class manages a sequence of objects Can grow and shrink as needed ArrayList class supplies methods for many common tasks, such as inserting and removing elements ArrayList is a generic class: ArrayList collects objects of type parameter T : ArrayList names = new ArrayList (); names.add("Emily"); names.add("Bob"); names.add("Cindy"); size method yields number of elements

10 To add an object to the end of the array list, use the add method: names.add("Emily"); names.add("Bob"); names.add("Cindy"); Adding Elements

11 To obtain the value an element at an index, use the get method 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 Retrieving Array List Elements

12 To set an element to a new value, use the set method: names.set(2, " Carolyn " ); Setting Elements

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

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

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

16 Working with Array Lists ArrayList names = new ArrayList (); Constructs an empty array list that can hold strings. names.add("Ann"); names.add("Cindy"); Adds elements to the end. System.out.println(names); Prints [Ann, Cindy]. names.add(1, "Bob"); Inserts an element at index 1. names is now [Ann, Bob, Cindy]. names.remove(0); Removes the element at index 0. names is now [Bob, Cindy]. names.set(0, "Bill"); Replaces an element with a different value. names is now [Bill, Cindy].

17 Working with Array Lists (cont.) String name = names.get(i); Gets an element. String last = names.get(names.size() - 1); Gets the last element. ArrayList squares = new ArrayList (); for (int i = 0; i < 10; i++) { squares.add(i * i); } Constructs an array list holding the first ten squares.

18 Syntax 7.2 Array Lists


Download ppt "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,"

Similar presentations


Ads by Google