CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]

Slides:



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

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Java POWERED BY: ARVIND DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING RADHA GOVIND GROUP OF INSTITUTIONS,MEERUT.
Generics and the ArrayList Class
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
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:
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Generic Classes and 'for each' Loops List zoo = new ArrayList (); // … add several Animals to the zoo for (Animal animal : zoo) // do something with animal.
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.
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:
Building Java Programs
ARRAYS AND ARRAYLISTS Chapter 7. Array  Sequence of values of the same type  Primitive types  Objects  Create an Array  double[] values = new double[10]
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
Arrays And ArrayLists - S. Kelly-Bootle
Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class.
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.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
ARRAYLIST Collections of Data. ArrayLists Array lists can grow and shrink as needed ArrayList is a generic class (similar to C++ template) ArrayList has.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
ArrayList, Multidimensional Arrays
Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Vectors, Strings, and Enumeration Data Types.
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.
Chapter 8: Arrays.
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
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.
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.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
CSC 142 P 1 CSC 142 Collections [Reading: Chapter 10]
OBJECTS FOR ORGANIZING DATA -- As our programs get more sophisticated, we need assistance organizing large amounts of data. : array declaration and use.
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.
JAVA COLLECTIONS M. TAIMOOR KHAN (ADAPTED FROM SWINBURNE NOTES)
CSE 143 Lecture 2 ArrayList reading: 10.1 slides created by Marty Stepp
Copyright 2010 by Pearson Education Building Java Programs Chapter 10, 11 Lecture 22: 143 Preview optional reading: 10.1,
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.
Comparing ArrayLists and Arrays. ArrayLists ArrayLists are one type of flexible-size collection classes supported by Java –ArrayLists increase and decrease.
JAC444: Intro to Java Arrays and Vectors Tim McKenna
Data Structures I Collection, List, ArrayList, LinkedList, Iterator, ListNode.
Collections Dwight Deugo Nesa Matic
The ArrayList Data Structure The Most Important Things to Review.
The ArrayList Data Structure Standard Arrays at High Speed!
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.
Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Java Arrays and ArrayLists COMP T1 #5
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Intro to Collections.
ARRAYLIST AND VECTOR.
Welcome to CSE 143!.
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.
ArrayLists.
Iterator.
Arrays versus ArrayList
ArrayList Collections.
Object Oriented Programming in java
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
CSC 142 Arrays [Reading: chapter 12].
Arrays and ArrayLists.
Presentation transcript:

CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]

The ArrayList class  Available in the package java.util.  An ArrayList object stores a list of items. The ArrayList automatically manages its size. The list grows as more items are added, or shrinks as some items are removed.  Items stored in an ArrayList all have the same reference type (i.e. defined by a class). Primitive type values can’t be stored as such in an ArrayList -> must use a wrapper type. CSC 142 J(part 1) 2

Declaring an ArrayList object  A named collection of data items of the same reference type.  To declare an ArrayList of Strings, write ArrayList phrases;  To declare an ArrayList of integers, write ArrayList numbers; NOT ArrayList numbers;  Always use the <> notation (= generics).  At this point, no memory is reserved for the ArrayList CSC 142 J(part 1) 3

Initializing an ArrayList  To initialize an ArrayList (once declared) phrases = new ArrayList (); numbers = new ArrayList ();  Other constructors are available e.g. // Make a copy of an existing ArrayList copy = new ArrayList (phrases); // Or if you know the size you need daysOfTheWeek = new ArrayList (7); // But daysOfTheWeek is NOT limited // to 7 elements CSC 142 J(part 1) 4

CSC 142 J(part 1) 5 Manipulating ArrayList elements (1)  To add an element to an ArrayList ArrayList a = new ArrayList (); a.add(“Monday”); a.add(“Wednesday”);  To access an ArrayList element, use its index (also called subscript), e.g., String first = a.get(0); // Monday String second = a.get(1); // Wednesday Indices start at 0. If an ArrayList has 10 elements, the first element has index 0 and the last element has index 9. To insert in an ArrayList a.add(1,“Tuesday”); //Wednesday is now at index 2

CSC 142 J(part 1) 6 Manipulating ArrayList elements (2)  To remove an element from an ArrayList String s = a.remove(1); // element at index 1 is // removed automatically, elements at indices > 1 // are shifted down // Also: boolean success = a.remove(s); // removes s (= some object) if present. Returns true // if s has been removed, and false if not.  Other operations: indexOf, contains, set See java documentation

CSC 142 J(part 1) 7 ArrayList and loops (1)  Loop with an index e.g. if a is an ArrayList of Rectangle objects for(int i = 0; i < a.size(); i ++) { Rectangle r = a.get(i); // work with r... }  Loop with an iterator Iterator it = a.iterator(); // could also use a ListIterator while(it.hasNext()) { Rectangle r = it.next(); // work with r... } Number of elements in a

ArrayList and loops (2)  for each loop for(Rectangle r : a) { // work with r... }  Which one to use?  To read the elements in no special order, use a for each loop.  To iterate through the ArrayList in a special way (e.g. from last to first, every other element), use a loop with an index.  for each and iterator loops are read only. CSC 142 J(part 1) 8

CSC 142 J(part 1) 9 Memory view ArrayList a= new ArrayList (); a.add(“Mercury”); a.add(“Earth”); a.add(1,”Venus”); a ArrayList String “Mercury” String “Venus” String “Earth”