Docsity

Java ArrayList: Properties, Constructors, Methods, and Complexity - Prof. Joel Castellanos, Study notes of Computer Science

Toggle sidebar
bg1
1
1/28/2009
Java ArrayList
CS 351
Design of Large Programs
Instructor: Joel Castellanos
Web: http://cs.unm.edu/~joel/
Office: Farris Engineering
Center (FEC) room 321
Lab Instructor: David Godinez
2
Java ArrayList
๎š„An ArrayList is a resizable-array of object
references in continuous memory.
๎š„Each ArrayList instance has a capacity. The capacity is
the size of the array used to store the elements in the
list.
๎š„The capacity is always at least as large as the list size.
๎š„As elements are added to an ArrayList, its capacity
grows automatically.
๎š„Details of the growth policy are not specified beyond
the fact that adding an element has constant amortized
time cost.
pf3
pf4

Partial preview of the text

Download Java ArrayList: Properties, Constructors, Methods, and Complexity - Prof. Joel Castellanos and more Study notes Computer Science in PDF only on Docsity!

1/28/

Java ArrayList CS 351

Design of Large Programs

Instructor: e-mail Web: http://cs.unm.edu/~joel/: [email protected] Joel Castellanos Lab Instructor:^ Office: Center (FEC) room 321^ Farris Engineering David Godinez e-mail: [email protected]

2

Java ArrayList

ย„ An references ArrayList in continuous memory is a resizable-array. of object

ย„ Each ArrayList instance has a capacity. The capacity isthe size of the array used to store the elements in thelist.

ย„ ย„ The capacity is always at least as large as the list size.As elements are added to an ArrayList, its capacity

ย„ grows automatically.Details of the growth policy are not specified beyond

the fact that adding an element has constant amortizedtime cost.

3

ArrayList Constructors (partial list) ย„ ArrayList() ย„ Constructs an empty list with an initial capacity often. ย„ ArrayList(int initialCapacity) ย„ Constructs an empty list with the specified initialcapacity. ย„ ArrayList(int initialCapacity) ย„ Constructs an empty list with the specified initialcapacity.

ย„ Allows the compiler to check that all adds andgets insert and return an object that subclasses classname.

4

Java ArrayList Methods (partial list) ย„ add(Object o)Appends the specified element to the end of thislist. ย„ add(int index, Object element)Inserts the specified element at the specifiedposition in this list. ย„ ObjectReturns the element at the specified position in thislist. get(int index) ย„ clear()Removes all elements from this list.

7

ArrayList: Access, Add and Insert

ArrayList.add(element): constant time ArrayList.add(index, element): O(n) time

A B C D E F A B C D E F X

A B C E E F A B C X D E F

An ArrayList is a continuous block of memory.The i th (^) element can be accessed in constant time.

8

LinkedList Access and Insertion

ย„ LinkedList.add(index, element): constant time

A B C D E

A B C D E

ย„ ย„ A LinkedList is, in general, a disjoint set of memory.The i th (^) element can be accessed in O ( n ) time.

X