The ArrayList Class An ArrayList object stores a list of objects, and is often processed using a loop The ArrayList class is part of the java.util package.

Slides:



Advertisements
Similar presentations
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;
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.
Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
For use of Cleveland State's IST410 Students only 1 Vectors and Collections.
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus Vector.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
DREW ALVAREZ AND CORDIE GOODRICH ARRAYS AND ARRAY LISTS.
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Outline ArrayList Searching Sorting Copyright © 2012 Pearson Education, Inc.
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.
ARRAYLIST.. Hazen High School. Vocabulary to Know ArrayList Generic Class ArrayList Operations ArrayList Methods ArrayList Searching For-Each Wrapper.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Arrays Construct array: new double[10] Store in variable of type double[] double[] data = new double[10];
Copyright 2008 by Pearson Education Building Java Programs ArrayList Reading: 10.1.
Chapter 7 Arrays: Part 2. © 2004 Pearson Addison-Wesley. All rights reserved2/27 Outline Declaring and Using Arrays Arrays of Objects Variable Length.
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.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
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,
Using Classes and Objects (Chapter 3) Copyright © 2012 Pearson Education, Inc.
CSE 1201 Object Oriented Programming ArrayList 1.
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.
1 CS162: Introduction to Computer Science II Abstract Data Types.
Exposure Java 2011 APCS Edition
An Array-Based Implementation of the ADT List
EKT472: Object Oriented Programming
CMSC 202 ArrayList Aug 9, 2007.
Lecture 23:More on Interfaces
Sixth Lecture ArrayList Abstract Class and Interface
(like an array on steroids)
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Intro to Collections.
ARRAYLIST AND VECTOR.
COP 3503 FALL 2012 Shayan Javed Lecture 8
Using Classes and Objects (Chapter 3)
Array List Pepper.
ArrayLists.
Programming in Java Lecture 11: ArrayList
Copyright ©2012 by Pearson Education, Inc. All rights reserved
(Java Collections Framework) AbstractSequentialList
CMSC 202 ArrayList Aug 9, 2007.
ArrayList Collections.
CMSC 202 ArrayList Aug 9, 2007.
Grouped Data Arrays, and Array Lists.
class PrintOnetoTen { public static void main(String args[]) {
ArrayLists 22-Feb-19.
The Random Class The Random class is part of the java.util package
slides created by Marty Stepp
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
ArrayLists.
ArrayLists 27-Apr-19.
slides created by Alyssa Harding
Chapter 2 : List ADT Part I – Sequential List
Review: libraries and packages
ArrayList.
Web Design & Development Lecture 6
Ps Module 7 – Part II 2D Arrays and LISTS 5/26/2019 CSE 1321 Module 7.
ArrayLists Readings: 10.1 (pg. 559 – 571).
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Presentation transcript:

The ArrayList Class An ArrayList object stores a list of objects, and is often processed using a loop The ArrayList class is part of the java.util package You can reference each object in the list using a numeric index An ArrayList object grows and shrinks as needed, adjusting its capacity as necessary Copyright © 2017 Pearson Education, Inc.

The ArrayList Class Index values of an ArrayList begin at 0 (not 1): 0 "Bashful" 1 "Sleepy" 2 "Happy" 3 "Dopey" 4 "Doc" Elements can be inserted and removed The indexes of the elements adjust accordingly Copyright © 2017 Pearson Education, Inc.

ArrayList Methods Some ArrayList methods: boolean add(E obj) void add(int index, E obj) Object remove(int index) Object get(int index) boolean isEmpty() int size() Copyright © 2017 Pearson Education, Inc.

The ArrayList Class The type of object stored in the list is established when the ArrayList object is created: ArrayList<String> names = new ArrayList<String>(); ArrayList<Book> list = new ArrayList<Book>(); This makes use of Java generics, which provide additional type checking at compile time An ArrayList object cannot store primitive types, but that's what wrapper classes are for See Beatles.java Copyright © 2017 Pearson Education, Inc.

//******************************************************************** // Beatles.java Author: Lewis/Loftus // // Demonstrates the use of a ArrayList object. import java.util.ArrayList; public class Beatles { //----------------------------------------------------------------- // Stores and modifies a list of band members. public static void main(String[] args) ArrayList<String> band = new ArrayList<String>(); band.add("Paul"); band.add("Pete"); band.add("John"); band.add("George"); continue Copyright © 2017 Pearson Education, Inc.

System.out.println(band); int location = band.indexOf("Pete"); continue System.out.println(band); int location = band.indexOf("Pete"); band.remove(location); System.out.println("At index 1: " + band.get(1)); band.add(2, "Ringo"); System.out.println("Size of the band: " + band.size()); int index = 0; while (index < band.size()) { System.out.println(band.get(index)); index++; } Copyright © 2017 Pearson Education, Inc.

Output [Paul, Pete, John, George] [Paul, John, George] At index 1: John Size of the band: 4 Paul John Ringo George continue System.out.println(band); int location = band.indexOf("Pete"); band.remove(location); System.out.println("At index 1: " + band.get(1)); band.add(2, "Ringo"); System.out.println("Size of the band: " + band.size()); int index = 0; while (index < band.size()) { System.out.println(band.get(index)); index++; } Copyright © 2017 Pearson Education, Inc.