array lists

18
Array Lists Chapter 7.2 Pages 248 - 253

Upload: steven-wall

Post on 30-Dec-2015

20 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Array Lists

Chapter 7.2Pages 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 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.

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

File8 30 2010 10009.738 27 2010 10150.658 26 2010 9985.818 25 2010 10060.068 24 2010 10040.458 23 2010 10174.418 20 2010 10213.62

RAM0 8 30 2010 10009.7316 8 27 2010 10150.6532 8 26 2010 9985.8148 8 25 2010 10060.06

…4816 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

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<T>

collects objects of type parameter T:

ArrayList<String> names = new ArrayList<String>();names.add("Emily");names.add("Bob");names.add("Cindy");

• size method yields number of elements

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

• 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

• To set an element to a new value, use the set method:

names.set(2, "Carolyn");

Setting Elements

• To remove an element at an index, use the remove method:

names.remove(1);

Removing Elements

• To remove an element at an index, use the remove method:

if (names.contains(“Carolyn”))

System.out.println(“Carolyn is here”);

Contains

names.add("Emily");names.add("Bob");names.add("Cindy");names.set(2, "Carolyn");names.add(1, "Ann");names.remove(1);

Adding and Removing Elements

Working with Array Lists

ArrayList<String> names = new ArrayList<String>();

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].

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<Integer> squares = new ArrayList<Integer>();for (int i = 0; i < 10; i++){ squares.add(i * i);}

Constructs an array list holding the first ten squares.

Syntax 7.2 Array Lists