ArrayList Collections.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
Hip Hip Array! AP Computer Science. Remember Strings? Strings are an array of characters An array is a collection of variables all of the same type. Arrays.
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.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
CSC 212 – Data Structures Lecture 21: IndexList a/k/a Vector, ArrayList.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
1 ArrayList  Array’s are limited because we need to know the size before we use them.  An ArrayList is an extension of an array that grows and shrinks.
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.
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
ArrayList, Multidimensional Arrays
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
Copyright 2008 by Pearson Education Building Java Programs ArrayList Reading: 10.1.
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.
ArrayList By Neil Butcher. What is the difference between an ArrayList and an Array? An ArrayList is in many ways similar to an array, but has a few subtle.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
CS 367 Introduction to Data Structures Lecture 2 Audio for Lecture 1 is available Homework 1 due Friday, September 18.
MCQ Which is the correct syntax for placing the string "boat" into an ArrayList name recVehicles in position 3 (index 2) for the first time? a)recVehicles.set(3,
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
ArrayList JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
Object-Oriented Programming (Java) Review Unit 1 Class Design Basic Console I/O StringTokenizer Exception UML class diagram.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
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.
Quiz: Design a Product Class Create a definition for a class called Product, which keeps track of the following information: –Name of the product –Weight.
1 CS162: Introduction to Computer Science II Abstract Data Types.
An Array-Based Implementation of the ADT List
EKT472: Object Oriented Programming
The List ADT Reading: Textbook Sections 3.1 – 3.5
(like an array on steroids)
Data Structures Lakshmish Ramaswamy.
Announcements & Review
COP 3503 FALL 2012 Shayan Javed Lecture 8
Implementing ArrayList Part 1
Top Ten Words that Almost Rhyme with “Peas”
Building Java Programs
TCSS 143, Autumn 2004 Lecture Notes
Programming in Java Lecture 11: ArrayList
Building Java Programs
Building Java Programs
The List ADT Reading: Textbook Sections 3.1 – 3.5
(Java Collections Framework) AbstractSequentialList
CSE 143 Lecture 27: Advanced List Implementation
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Java For-Each loop A delightful shortcut.
Lecture 2: Implementing ArrayIntList reading:
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Grouped Data Arrays, and Array Lists.
ArrayLists 22-Feb-19.
Collections Framework
Collections and iterators
Review of Previous Lesson
JCF Collection classes and interfaces
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
ArrayLists 27-Apr-19.
TCSS 143, Autumn 2004 Lecture Notes
Collections and iterators
Arrays and ArrayLists.
Presentation transcript:

ArrayList Collections

How Do I Use an ArrayList? First, import it so it is available for use: import java.util.ArrayList; -or- import java.util.*;

How Do I Use an ArrayList? Next declare and initialize! ArrayList<Point> myPoints; myPoints = new ArrayList<Point>(); //note: The initial capacity of an ArrayList is 10 Recall the Java array style Point[] myPoints; myPoints = new Points[10];

How do I add items? Call the add method (adds the item to the back of the ArrayList). Point p = new Point(1,2); myPoints.add(p); myPoints.add(new Point(2,3));

How many items are in the ArrayList? For an ArrayList, we use myPoints.size() For an array, we use myPointsArr.length Note: size() gives number of items, length gives the capacity

How do I retrieve items? For an ArrayList For an array, Point p = myPoints.get(0); For an array, Point p = myPointArr[0];

Example (Traversal and set) Given an ArrayList called myNames filled with names, replace all occurrences of “marc” with “mark”. for(int i = 0; i < myNames.size(); i++) { if (myNames.get(i).equals(“marc”)) myNames.set(i, “mark”); } Note: The set method replaces what is at i with the new element and returns the old element (which is ignored in this case).

Traversal with “for each” loop for(String s: myNames) { if (myNames.get(i).equals(“marc”)) myNames.set(i, “mark”); }

Can elements only be added at the back of the ArrayList? No! You can add an element at any valid index. Consider the ArrayList named myAlphabet: [“apple”, “bear”, “cake”, “dog”] The call myAlphabet.add(2, “zebra”); results in [“apple”,“bear”,“zebra”,“cake”,“dog”] Note: No need to shift things over by hand! ArrayList does this for you!!!

Special note on add Given the following ArrayList named myAlphabet: [“apple”, “bear”, “cake”, “dog”] The following two calls would produce identical results: myAlphabet.add(“zebra”) myAlphabet.add(4, “zebra”); Note: myAlphabet.add(5, “zebra”) will cause an IndexOutOfBoundsException.

Easy to remove as well! [“apple”, “bear”, “cake”, “dog”] Remember the pain of removing with arrays (having to shift elements to the left). ArrayList does this for you! Given [“apple”,“bear”,“zebra”,“cake”,“dog”] myAlphabet.remove(2) results in [“apple”, “bear”, “cake”, “dog”]

Summary of ArrayList methods class java.util.ArrayList<E> int size() boolean add(E e) appends e to end of list, returns true void add(int i, E e) inserts e at index of i (0 <= i <= size) moving elements right as necessary E get(int i) returns element at index of i E set(int i,E e) replaces element at i with e, returns former element at i E remove(int i) removes element at I moving elements left as necessary.

Arrays vs. ArrayList Arrays ArrayList Declaration/Initialization Point[] p; p = new Point[5]; ArrayList<Point> p; p=new ArrayList<Point>(); Assignment (replace) p[3]=aPoint; p.set(3, aPoint); Insertion Lots of code to shift element right p.add(2, aPoint); Deletion Lots of code to shift elements left p.remove(2); Number of items p.length p.size(); Access p[4].getX(); p.get(4).getX();