ArrayLists.  The Java API includes a class named ArrayList. The introduction to the ArrayList class in the Java API documentation is shown below.  java.lang.Object.

Slides:



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

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Chapter 10 Introduction to Arrays
Programming with Collections Collections in Java Using Arrays Week 9.
1 Java intro Part 3. 2 Arrays in Java Store fixed number of values of a given type Arrays are objects –have attributes –must be constructed Array declaration:
Loops Notes adapted from Dr. Flores. It repeats a set of statements while a condition is true. while (condition) { execute these statements; } “while”
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Grouping objects Collections and iterators. 04/11/2004Lecture 4: Grouping Objects2 Main concepts to be covered Collections Loops Iterators Arrays.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
Java Unit 9: Arrays Declaring and Processing Arrays.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Chapter 28 Iterator Summary prepared by Kirk Scott 1.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
Chapter 11 Arrays Continued
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
1 Object-Oriented Programming (Java), Unit 9 (comes before Unit 20) Kirk Scott.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Grouping objects Arrays, Collections and Iterators 1.0.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Java SE 8 for Programmers, Third Edition
Chapter 18 Java Collections Framework
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
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.
1 Generics Chapter 21 Liang, Introduction to Java Programming.
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.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
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.
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.
Iterators ITI 1121 N. El Kadri. Motivation Given a (singly) linked-list implementation of the interface List, defined as follows, public interface List.
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
COMP More About Arrays Yi Hong June 05, 2015.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
The ArrayList Data Structure The Most Important Things to Review.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Lecture 5:Interfaces and Abstract Classes Michael Hsu CSULA.
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
Lecture 6:Interfaces and Abstract Classes Michael Hsu CSULA.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Sections 10.1 – 10.4 Introduction to Arrays
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 20 Generic Classes and Methods
Summary prepared by Kirk Scott
Arrays, For loop While loop Do while loop
Iterator.
Object Oriented Programming in java
Collections and iterators
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Collections and iterators
Presentation transcript:

ArrayLists

 The Java API includes a class named ArrayList. The introduction to the ArrayList class in the Java API documentation is shown below.  java.lang.Object ◦ java.util.AbstractCollection  java.util.AbstractList  java.util.ArrayList

 The ArrayList class is sort of a hybrid. As its name implies, it has array-like characteristics, as well as characteristics that can be described as list-like.  It has a wide variety of methods that allow you to manipulate its elements in many different ways.

 In the interests of brevity, none of these methods are presented here.  Some will be used in the example code which will follow. If necessary, you should take a look in the Java API documentation to see what methods are available and to get information on the use of specific methods.

 In order to use an ArrayList in a program it has to be imported with the following statement: ◦ import java.util.ArrayList;

 The declaration of an ArrayList can use angle bracket notation to specify what kinds of elements it contains. For example, the following line of code in a program would declare and construct an instance of an ArrayList with the name myCollection that contained elements which were references to instances of the Cup7 class: ◦ ArrayList myCollection = new ArrayList ();

 If it is desirable for the ArrayList to contain simple types like integers or doubles, the values should be stored in instances of the wrapper classes Integer and Double, and references to these objects can be elements of the ArrayList. ◦ ArrayList yourCollection = new ArrayList (); ◦ It’s using the ‘Double’ class, not the ‘double’ type.

 To traverse all of the elements of a simple array, it would be customary to use a for loop where the loop index cycles through values for the array subscript.

 An ArrayList is different. Its elements can be traversed using a “for each” loop. Here is a simple example illustrating the “for each” syntax. Note that in reality, the word “each” does not appear in it. It is given this name to distinguish it from a simple for loop. ◦ For(Cup7 someCup : myCollection) { System.out.println(someCup); }

 In the for statement a local reference named “someCup” is declared.  The for statement also contains a colon followed by the name of the ArrayList to be accessed, in this example named myCollection.  The type of the local reference agrees with the type of the elements of the ArrayList. The loop runs through all of the elements of myCollection, successively assigning references of its elements to the local reference, someCup.  Notice that the loop delivers access to successive elements of the collection, but it is not possible to call methods on the collection itself.  This means that it is not possible to add elements to, or remove them from the collection when using this syntax.

 Traversing the elements of a collection like an ArrayList is known as iteration.  The Java API includes interfaces named Iterator and Iterable. These are specifications that support working with the elements of an ArrayList while traversing it.  The Iterable interface contains one method, named iterator(). Calling this method on an ArrayList returns a reference to an iterator.  When obtaining the iterator, in order for your code to be consistent, you want to use angle bracket notation to specify what kinds of elements the iterator is dealing with.  If myCollection contains Cup7 elements, then declaring and obtaining an iterator for it would take this form: ◦ Iterator myIterator = myCollection.iterator();

 Three different methods can be called on an iterator: ◦ boolean hasNext()  Returns true if the iteration has more elements. ◦ E next()  Returns the next element in the iteration. ◦ void remove()  Removes from the underlying collection the last element returned by the iterator (optional operation).  These three methods make it possible to iterate through an ArrayList checking to see whether there is a “next” element, getting that element if there is one, and removing it if desired.

 Here is an example using a while loop based on myCollection containing references to the Cup7 class. ◦ Iterator myIterator = myCollection.iterator(); Cup7 someCup; while(myIterator.hasNext()) { someCup = myIterator.next(); if (someCup.getSeedCount() <= 0) myIterator.remove(); }

 The iterator for the collection is obtained before the loop, and the test for continuing to loop is a call to hasNext().  In the body of the loop, the call to next() returns a reference to the next element, and the call to remove() causes that element to be removed from the collection.  The example removes from myCollection all elements, all cups, which contain 0 or fewer seeds.

 The following example accomplishes the same thing more compactly using a for loop. ◦ for(Iterator myIterator = myCollection.iterator(); myIterator.hasNext();) { if((myIterator.next().getSeedCount()) <= 0) myIterator.remove(); }

 The first part of the for statement obtains the iterator for the collection.  The second part of the for statement causes the iteration to continue as long as there is another element in the collection.  Notice that the for loop is structured to contain three different things separated by semicolons, but it only contains two things and there is a concluding semicolon before the closing parenthesis.  The body of this loop is more compact than the while loop example. It removes an element without making use of a temporary reference.