The ArrayList Class An ArrayList is a complex data structure that allows you to add or remove objects from a list and it changes size automatically. The.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

Two Dimensional Arrays and ArrayList
SD2054 Software Development. The Limitation of Arrays Once an array is created it must be sized, and this size is fixed!
Generics and the ArrayList Class
Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter 7 Arrays. © 2004 Pearson Addison-Wesley. All rights reserved7-2 Arrays Arrays are objects that help us organize large amounts of information Chapter.
Chapter 6: Arrays & More GUI Copyright 2002, Matthew Evett. These slides are based on slides copyrighted by John Lewis and William Loftus, 2002, and used.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
© The McGraw-Hill Companies, 2006 Chapter 17 The Java Collections Framework.
Chapter 4 Linked Structures. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 4-2 Chapter Objectives Describe the use of references to create.
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
1 More on Arrays Arrays of objects Command line arguments The ArrayList class Javadoc Review Lecture 8 notes and L&L 7.1 – 7.2 Reading for this lecture:
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Data structures Abstract data types Java classes for Data structures and ADTs.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
The Java Collections Framework By the end of this lecture you should be able to: use the ArrayList class to store a list of objects; use the HashSet class.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
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.
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
List Interface and Linked List Mrs. Furman March 25, 2010.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
The ArrayList Data Structure The Most Important Things to Review.
The ArrayList Data Structure Standard Arrays at High Speed!
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
More on Arrays Review of Arrays of ints, doubles, chars
An Array-Based Implementation of the ADT List
EKT472: Object Oriented Programming
CMSC 202 ArrayList Aug 9, 2007.
Chapter 4 Linked Structures.
Sixth Lecture ArrayList Abstract Class and Interface
Objects First with Java Introduction to collections
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Intro to Collections.
COMP 121 Week 9: ArrayList.
ARRAYLIST AND VECTOR.
Array Array is a variable which holds multiple values (elements) of similar data types. All the values are having their own index with an array. Index.
Arrays and the ArrayList Class The ArrayList Class
Chapter-7 part3 Arrays Two-Dimensional Arrays The ArrayList Class.
Welcome to CSE 143!.
Array List Pepper.
Linked Lists.
Programming in Java Lecture 11: ArrayList
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 8 Slides from GaddisText
Arrays versus ArrayList
CMSC 202 ArrayList Aug 9, 2007.
Abstract Class As per dictionary, abstraction is the quality of dealing with ideas rather than events. For example, when you consider the case of ,
Welcome to CSE 143!.
Arrays and Collections
Dynamic Data Structures and Generics
Object Oriented Programming in java
CMSC 202 ArrayList Aug 9, 2007.
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Dr. Sampath Jayarathna Cal Poly Pomona
Review: libraries and packages
Arrays.
Presentation transcript:

The ArrayList Class An ArrayList is a complex data structure that allows you to add or remove objects from a list and it changes size automatically. The ArrayList class is part of the java.util package of the Java standard class library. It works like an array in that it can store a list of values and reference them by an index. But an array remains a fixed size throughout its existence, and an arraylist object grows and shrinks as needed.

The ArrayList Class The ArrayList class is part of the java.util package It must be imported Like an array, it can store a list of values and reference them with an index Unlike an array, an ArrayList object grows and shrinks as needed Items can be inserted or removed with a single method invocation It stores references to the Object class, which allows it to store any kind An ArrayList is an object of the ArrayList class. Therefore, to create an ArrayList, you need to use the keyword new along with the constructor from the ArrayList class. You also need to know the data type of the objects that will be stored in the list.

DestinysChild.java The DestinysChild program instantiates an ArrayList called band. The method add is used to add several String objects to the ArrayList in a particular order. Then one string is deleted and another is inserted at a particular index. As with any other object, the toString method of the ArrayList class is automatically called when it is sent to the println method. Note that when an element from an ArrayList is deleted, the mist of elements collapses so that there are no holes in the list. Likewise, when an element is inserted the list expands.

Declaring an ArrayList Object Two general forms of declaring anArrayList: ArrayList<ClassName> nameOfArrayList = new ArrayList<ClassName>(); Or List<ClassName> nameofArrayList = ArrayList<ClassName>(); The ArrayList uses a pair of angle brackets to enclose the class name of the objects it will store. An ArrayList always starts out empty. When you create an ArrayList object, it is empty, meaning that there is no item in the list. It’s like when your mom starts to make a “To Do” list and she writes the words “To Do” on the top of a piece of paper. The list is created but there is nothing in the list. An ArrayList is resizable. When your mom writes, “Go grocery shopping” or “Buy awesome video game for favorite child” on her to do list, the size of the list grows. As she completes a task on the list, she crosses it out and the size of the list shrinks. This is exactly how an ArrayList is resized.

Specifying an ArrayList Element Type ArrayList is a generic type, which allows us to specify the type of data each ArrayList should hold For example, ArrayList<Family> holds Family objects

Recipe.java The recipe program declares an ArrayList<String>. It stores and prints a list of ingredients for a recipe. The elements are added with the .add method. The list Iterator method on ArrayList returns a ListIterator object that can be used to iterate through the items in the list. Once the iterator is obtained from the ArrayList, the hasNext and next methods are used to loop through each of the elements in the list. We could have also used a foreach loop here.

ArrayList Efficiency The ArrayList class is implemented using an array The code of the ArrayList class automatically expands the array's capacity to accommodate additional elements The array is manipulated so that indexes remain continuous as elements are added or removed If elements are added to and removed from the end of the list, this processing is fairly efficient If elements are inserted and removed from the middle of the list, the elements are constantly being shifted around