Programming in Java Lecture 11: ArrayList

Slides:



Advertisements
Similar presentations
Linked Lists Linear collections.
Advertisements

CSC 205 – Java Programming II Lecture 25 March 8, 2002.
5-May-15 ArrayLists. 2 ArrayList s and arrays A ArrayList is like an array of Object s Differences between arrays and ArrayList s: Arrays have special.
CS 106 Introduction to Computer Science I 04 / 30 / 2007 Last lecture :( Instructor: Michael Eckmann.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
CS 106 Introduction to Computer Science I 04 / 27 / 2007 Instructor: Michael Eckmann.
Java Collections Framework COMP53 Oct 24, Collections Framework A unified architecture for representing and manipulating collections Allows collections.
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
15-Jun-15 Lists in Java Part of the Collections Framework.
For use of Cleveland State's IST410 Students only 1 Vectors and Collections.
What Is a Collection?  A collection (sometimes called a container) is simply an object that groups multiple elements into a single unit.  Collections.
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus Vector.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
25-Jun-15 Vectors. 2 Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: Arrays have special syntax; Vector.
Vectors. Vectors and arrays A Vector is like an array of Object s Differences between arrays and Vector s: –Arrays have special syntax; Vector s don’t.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
12-Jul-15 Lists in Java Part of the Collections Framework.
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
04/29/ Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
ArrayList, Multidimensional Arrays
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
Copyright 2008 by Pearson Education Building Java Programs ArrayList Reading: 10.1.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
List data type(ADT). Lists Elements : a 1,a 2,a 3,… a i-1,a i, a i+1,…a n Null List contains: 0 elements Types of Operations on list 1.Insertion 2.Deletion.
CS1020 Data Structures and Algorithms I Lecture Note #6 Vector and ArrayList.
Object Oriented Programming in Java Habib Rostami Lecture 7.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
1 CS162: Introduction to Computer Science II Abstract Data Types.
An Array-Based Implementation of the ADT List
Fundamental of Java Programming
EKT472: Object Oriented Programming
Data Structures Lakshmish Ramaswamy.
COP 3503 FALL 2012 Shayan Javed Lecture 8
Implementing ArrayList Part 1
Introduction to Collections
TCSS 143, Autumn 2004 Lecture Notes
Data Type (how to view it)
Introduction to Collections
(Java Collections Framework) AbstractSequentialList
ArrayList Collections.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Object-Oriented Programming Paradigm
L2. Necessary Java Programming Techniques
Grouped Data Arrays, and Array Lists.
Introduction to Collections
ArrayLists 22-Feb-19.
Collections Framework
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
ArrayLists.
ArrayLists 27-Apr-19.
ArrayList.
មជ្ឈមណ្ឌលកូរ៉េ សហ្វវែរ អេច អ ឌី
TCSS 143, Autumn 2004 Lecture Notes
Part of the Collections Framework
Presentation transcript:

Programming in Java Lecture 11: ArrayList

Introduction Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means we can not resize Arrays. Arrays are useful when we know in advance how many elements the array is to hold. ArrayList is a collection which provides the implementation of resizable array.

ArrayList The ArrayList class extends AbstractList and implements the List interface. Defined in java.util package. ArrayList supports dynamic arrays that can grow as needed. Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.

ArrayList Constructors T he ArrayList class supports three constructors. ArrayList() : creates an empty array list with an initial capacity sufficient to hold 10 elements. ArrayList(int capacity) : creates an array list that has the specified initial capacity. ArrayList(Collection c) : creates an array list that is initialized with the elements of the collection c.

void add(int index, Object element) Methods of ArrayList void add(int index, Object element) Inserts the specified element at the specified position index in this list. Throws IndexOutOfBoundsException if the specified index is is out of range.

boolean addAll(Collection c ) boolean add(Object o) Appends the specified element to the end of this list. boolean addAll(Collection c ) Appends all of the elements in the specified collection to the end of this list. Throws NullPointerException if the specified collection is null. boolean addAll(int index, Collection c ) Inserts all of the elements of the specified collection into this list, starting at the specified position.

Object remove(int index) void clear() Removes all of the elements from this list. Object remove(int index) Removes the element at the specified position in this list. Object clone() Returns a shallow copy of this ArrayList. int size() Returns the number of elements in the list.

int lastIndexOf(Object o) boolean contains(Object o) Returns true if this list contains the specified element. Object get(int index) Returns the element at the specified position in this list. int indexOf(Object o) Returns the index in this list of the first occurrence of the specified element, or -1 if the List does not contain the element. int lastIndexOf(Object o) Returns the index in this list of the last occurrence of the specified element, or -1.

void ensureCapacity(int minCapacity) Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.

Example

Example