TCSS 143, Autumn 2004 Lecture Notes

Slides:



Advertisements
Similar presentations
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
Advertisements

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.
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.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
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.
Collections The objectives of this chapter are: To outline the Collections infrastructure in Java To describe the various collection classes To discuss.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
Building Java Programs
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
AP CS Workshop ArrayList It is very common for applications to require us to store a large amount of data. Array lists store large amounts of data.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
CS 106 Introduction to Computer Science I 04 / 25 / 2008 Instructor: Michael Eckmann.
CSE 143 Lecture 2 ArrayList reading: 10.1 slides created by Marty Stepp
1 TCSS 342, Winter 2005 Lecture Notes Linked List Implementation Weiss Ch. 6, pp Weiss Ch. 17, pp
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
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.
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
1 The copy constructor in the BankAccounts class. Two alternatives here: /** copy constructor */ public BankAccounts(BankAccounts L){ theAccounts = L.theAccounts.clone();
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.
EKT472: Object Oriented Programming
slides created by Marty Stepp
Collections.
Linked Lists Chapter 5 (continued)
Data Structures Lakshmish Ramaswamy.
COP 3503 FALL 2012 Shayan Javed Lecture 8
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Welcome to CSE 143!.
Java Array Lists 2/13/2017.
Introduction to Collections
ArrayLists.
Programming in Java Lecture 11: ArrayList
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Introduction to Collections
ArrayLists.
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
Chapter 10 ArrayList reading: 10.1
Welcome to CSE 143!.
Collections in Java The objectives of this lecture are:
Grouped Data Arrays, and Array Lists.
Computer Science and Engineering
Introduction to Collections
Java Array Lists 2/13/2017.
ArrayLists 22-Feb-19.
Collections Framework
Review of Previous Lesson
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Linked Lists Chapter 5 (continued)
ArrayLists.
ArrayLists 27-Apr-19.
Review: libraries and packages
ArrayList.
slides created by Marty Stepp
TCSS 143, Autumn 2004 Lecture Notes
Part of the Collections Framework
Linked Lists Chapter 5 (continued)
Java Generics & Iterators
Copyright ©2012 by Pearson Education, Inc. All rights reserved
What can you put into an ArrayList?
Presentation transcript:

TCSS 143, Autumn 2004 Lecture Notes Introduction to Collections: Array List

Collections collection: an object that stores a group of objects inside it; a.k.a. a "data structure" the objects stored are called elements some collections allow duplicates, some don't some maintain an ordering, some don't an array is like a very crude "collection" typical operations: add element, remove element, clear all elements, contains or find element, get size examples: java.util.ArrayList, java.util.HashMap, java.util.TreeSet

Collection interface Java provides an interface java.util.Collection with the following methods: public boolean add(Object o) Appends the specified element to this collection. public void clear() Removes all of the elements of this collection. public boolean contains(Object o) Returns true if this collection contains the specified element. public boolean containsAll(Collection coll) Returns true if this collection contains all of the elements in the specified collection.

Collection interface, cont'd. public boolean isEmpty() Returns true if this list contains no elements. public Iterator iterator() Returns a special object for examining the elements of the list in order (seen later). public boolean remove(Object o) Removes the first occurrence in this list of the specified element. public int size() Returns the number of elements in this list. public Object[] toArray() Returns an array containing all of the elements from this list.

Lists list: an ordered sequence of elements, each accessible by a 0-based index one of the most basic collections

List features maintains ordering in order elements were added (new elements are added to the end by default) duplicates and null elements allowed list manages its own size; user of the list does not need to worry about overfilling it operations: add element to end of list, insert element at given index, clear all elements, search for element, get element at given index, remove element at given index, get size

List interface Java interface java.util.List adds the following methods to those in Collection: public void add(int index, Object o) Inserts the specified element at the specified position in this list. public Object get(int index) Returns the element at the specified position in this list. public 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 it.

List interface, cont'd. More java.util.List methods: public int lastIndexOf(Object o) Returns the index in this list of the last occurrence of the specified element, or -1 if the list does not contain it. public Object remove(int index) Removes the object at the specified position in this list. public Object set(int index, Object o) Replaces the element at the specified position in this list with the specified element.

Some list questions all of the list operations on the previous slide could be performed using an array instead! open question: What are some reasons why we might want to use a special List class, rather than an array, to store our data? thought question: How might a List be implemented, under the hood? why do all the List methods use type Object?

ArrayList Class java.util.ArrayList implements List using an array as the internal implementation

ArrayList, cont'd. ArrayList features think of it as an auto-resizing array, that can hold any type of object, with many convenient methods maintains most of the benefits of arrays, such as fast random access can call toString on an ArrayList to print it remember to import java.util.*;

ArrayList vs. array construction storage retrieval String[] names = new String[5]; ArrayList namesList = new ArrayList(); storage names[0] = "Jennifer"; namesList.add("Jennifer"); retrieval String name = names[0]; String name = (String)namesList.get(0); Why the type-cast? What happens without it?

ArrayList vs. array, cont'd. removal (of element #2) for (int i = 2; i < names.length - 1; i++) names[i] = names[i+1]; namesList.remove(2); search to see if "Marty" is there for (int i = 0; i < names.length; i++) if (names[i].equals("Marty")) { ... } if (namesList.contains("Marty")) { ... } erase all names from the list for (int i = 0; i < names.length; i++) names[i] = null; namesList.clear();

ArrayList and references a collection such as ArrayList stores references to its elements if two collections refer to the same object, side effects from one can be seen in the other BankAccount acct = new BankAccount("Ed", 0); ArrayList list1 = new ArrayList(); list1.add(acct); ArrayList list2 = new ArrayList(); list2.add(acct); acct.deposit(100.00); System.out.println(list1); System.out.println(list2);

Storing primitives in collection collections like ArrayList store Objects, not primitive values like int, double, char, boolean to store these primitives, we need special object wrappers Integer / public int intValue Double / public double doubleValue Character / public char charValue Boolean / public boolean booleanValue ArrayList list = new ArrayList(); list.add(new Integer(42)); int value = ((Integer)list.get(0)).intValue();

Collections class Class java.util.Collections has many utility methods that do useful things to collections public static void copy(List dest, List src) public static void fill(List list, Object value) public static Object max(Collection coll) public static Object min(Collection coll) public static void reverse(List list) public static void shuffle(List list) public static void sort(List list) public static void swap(List list, int i, int j)

Abstract data types (ADTs) abstract data type: a specification for a collection of data and the operations that can be performed on it does not specify how the collection must be implemented internally does specify what ways the data can be viewed and manipulated (what methods the ADT has) many collections may implement the same ADT (example: linked list and array list implement List) in Java, interfaces specify ADTs, and classes implement the specific collections

Practice problems Using Scanner, read names from the keyboard until an empty line is entered. Store the names in an ArrayList, and print the names separated by commas after all have been entered. Use an ArrayList to store and print the numbers 1-50 in random order, one per line.

References Koffman, Chapter 4, pp. 193-199