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.

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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
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.
15-Jun-15 Lists in Java Part of the Collections Framework.
1 Dynamic Arrays  Why Dynamic Arrays?  A Dynamic Array Implementation  The Vector Class  Program Example  Array Versus Vector.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
1 ADTs, Collection, Iterable/Iterator Interfaces Collections and the Java Collections API The Collection Interface and its Hierarchy The Iterable and Iterator.
Arrays And ArrayLists - S. Kelly-Bootle
Session 5 java.lang package Using array java.io package: StringTokenizer, ArrayList, Vector Using Generic.
Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class.
Copyright © Texas Education Agency, Advanced Computer Programming Data Structures: Collections.
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.
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
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.
Java SE 8 for Programmers, Third Edition
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
Lecture objectives  Collections interface  Learn about stacks and their methods:  push  pop  peek  Empty  Analyze stack applications and why stacks.
Copyright 2008 by Pearson Education Building Java Programs ArrayList Reading: 10.1.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Java Class Library: The Interface.
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.
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.
AP Computer Science edition Review 1 ArrayListsWhile loopsString MethodsMethodsErrors
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
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,
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
Click to edit Master title style Click to edit Master text styles Second level Third level Fourth level Fifth level 1 ArrayLists Section 9.9.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
CSE 1201 Object Oriented Programming ArrayList 1.
ArrayList JavaMethods An Introduction to Object-Oriented Programming Maria Litvin Gary Litvin Copyright © 2003 by Maria Litvin, Gary Litvin, and Skylight.
Lecture 9: Lists MIT-AITI Kenya © 2005 MIT-Africa Internet Technology Initiative In this lecture we will learn…. ArrayList – These are re-sizeable.
Object-Oriented Programming (Java) Review Unit 1 Class Design Basic Console I/O StringTokenizer Exception UML class diagram.
CS1020 Data Structures and Algorithms I Lecture Note #6 Vector and ArrayList.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
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.
The ArrayList Data Structure Standard Arrays at High Speed!
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.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
1 CS162: Introduction to Computer Science II Abstract Data Types.
Array Lists and Arrays Sections 13.1, 13.3 Common Errors 13.1, 13.2
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Lists Chapter 4.
COP 3503 FALL 2012 Shayan Javed Lecture 8
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.
TCSS 143, Autumn 2004 Lecture Notes
Programming in Java Lecture 11: ArrayList
(Java Collections Framework) AbstractSequentialList
ArrayList Collections.
Grouped Data Arrays, and Array Lists.
ArrayLists 22-Feb-19.
Review of Previous Lesson
JCF Collection classes and interfaces
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
ArrayList.
Presentation transcript:

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 as needed.  The ArrayList class is part of the java.util package of the Java standard class library.

2 ArrayLists contain Objects  Numbers are not objects.  Unlike an array, you can not have an ArrayList of int s or d ouble s or b oolean s or char s.

3 ArrayList  Each element in the sequence can be accessed separately.  We can explicitly overwrite an object at a specified position in the sequence, thus changing its value.  We can inspect the object at a specified location in the sequence.  We can add an object into a specified position of the sequence.  We can add an object to the end of the sequence.  We can remove an object from a specified location in the sequence.

4 ArrayList s for AP CS A Notice that the AP CS A Subset requires the knowledge that ArrayList implements List.

5 java.util.ArrayList for AP CS A  int size() // returns the number of elements in this list  boolean add(E x) // appends x to the end of list; returns true  E get(int index) // returns the element at the specified position in this list.  E set(int index, E x) // replaces the element at index with x // returns the element formerly at the specified position

6 class java.util.ArrayList for AP CS A  void add(int index, E x) // inserts x at position index, sliding elements // at position index and higher to the right // (adds 1 to their indices) and adjusts size  E remove(int index) // removes element from position index, sliding // subsequent elements to the left (subtracts 1 from their // indices) and adjusts size // returns the element at the specified position in this list.

7 An example: import java.util.ArrayList; public class ArrayListTest { public ArrayListTest { System.out.println("ArrayListTest"); ArrayList aList = new ArrayList (); aList.add("Dan"); aList.add("George"); aList.add("Mary"); System.out.println("aList contains:"); for(int x = 0; x < aList.size(); x++) { System.out.println(aList.get(x)); } } }

8 Using enhanced FOR loop: import java.util.ArrayList; public class ArrayList_01_ForEach { public static void main (String [] arg) { System.out.println("ArrayListTest"); ArrayList aList = new ArrayList (); aList.add(new String("Dan")); aList.add("George"); aList.add("Mary"); System.out.println("aList contains:"); for(String e:aList) { System.out.println(e); } } }

9 Other ArrayList methods // ArrayListTest2 ArrayList students = new ArrayList (); students.add("Mary" ); students.add("James"); students.add("Kevin"); students.add(1, "Tanya"); String temp = students.get(3); System.out.println(temp); students.remove(2); students.set(1, "John"); System.out.println(students.size());

10 What happens? ArrayList_02 ArrayList students = new ArrayList (); students.add("Mary"); students.add("James"); students.add("Kevin"); students.add(1, "Tanya"); String temp = students.get(3); System.out.println(temp); students.remove(2); students.set(1, "John"); System.out.println(students.size()); MaryJamesKevin MaryTanyaJamesKevin temp MaryTanyaKevin MaryJohnKevin

11 An ArrayList is a sequence of objects.  Array lists can hold any kind of object. For the generic ArrayList, you must include the type of object the ArrayList will hold. ArrayList athletes = new ArrayList (); ArrayList csci6 = new ArrayList (); ArrayList accounts = new ArrayList ();  The ArrayList method add adds an Object of the type specified in the array list declaration.  The ArrayList method get returns an Object of the type specified in the array list declaration.

12 What happens? import java.util.ArrayList; public class StringTest { public StringTest() { ArrayList stringList = new ArrayList (); stringList.add("Fran"); stringList.add("Marie"); stringList.add("Joe"); System.out.println(stringList); } }