AP CS Workshop 20081 ArrayList It is very common for applications to require us to store a large amount of data. Array lists store large amounts of data.

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.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
Chapter 7 – Arrays.
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.
Datalogi A 8: 27/10. Array Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] double[] data.
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.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
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.
Java™ How to Program, 9/e Presented by: Dr. José M. Reyes Álamo © Copyright by Pearson Education, Inc. All Rights Reserved.
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.
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
Generalized Containers CSIS 3701: Advanced Object Oriented Programming.
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]
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.
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.
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
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.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
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.
1 ArrayList Starring: Purse, Sr. Co-Starring: Purse, Jr. Poe Numbers.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
Array Objectives To understand the concept of arrays To understand the purpose to which we use arrays. To be able to declare references to arrays. To be.
Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and Skylight.
1 ArrayList Starring: Purse, Sr. Co-Starring: Purse, Jr. Poe Numbers.
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
ArrayLists (and the for-each loop) ArrayList example = new ArrayList (); example.add(4);
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
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.
An Introduction to Java – Part 1 Erin Hamalainen CS 265 Sec 001 October 20, 2010.
int [] scores = new int [10];
CS1020 Data Structures and Algorithms I Lecture Note #6 Vector and ArrayList.
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.
Comp1004: Environments The Java Library. Coming up Recap – Encapsulation – Constructors – Loops – Arrays – ArrayList – Iterators The Java Library – Implementation.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Array Lists and Arrays Sections 13.1, 13.3 Common Errors 13.1, 13.2
Intro to Collections.
Multiple variables can be created in one declaration
ARRAYLIST AND VECTOR.
COP 3503 FALL 2012 Shayan Javed Lecture 8
TCSS 143, Autumn 2004 Lecture Notes
Can store many of the same kind of data together
ArrayLists.
ArrayList Collections.
Grouped Data Arrays, and Array Lists.
ArrayLists 22-Feb-19.
Can store many of the same kind of data together
Review of Previous Lesson
Arrays.
Java Coding 6 David Davenport Computer Eng. Dept.,
What can you put into an ArrayList?
Presentation transcript:

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 in a single collection that can be referred to with a single variable.

AP CS Workshop ArrayList An ArrayList is a sequence of objects that grows and shrinks as needed. The ArrayList class is part of the java.util package of the Java standard class library. –must import java.util.ArrayList

AP CS Workshop 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.

AP CS Workshop java.util.ArrayList (implements List) ArrayList is generic ArrayList

AP CS Workshop ArrayList s for AP CS A Notice that the AP CS A Subset does not require the knowledge that ArrayList implements List.

AP CS Workshop 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

AP CS Workshop 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.

AP CS Workshop Course Description AP CS AB Notice that the AB Subset DOES require the knowledge that ArrayList implements List. iterators SHOULD BE TAUGHT in A

AP CS Workshop An example: import java.util.ArrayList; public class ArrayList_01 { 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(int x = 0; x < aList.size(); x++) { System.out.println(aList.get(x)); }

AP CS Workshop 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); }

AP CS Workshop Other ArrayList methods // 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());

AP CS Workshop 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

AP CS Workshop 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.

AP CS Workshop A bit more about toString()

AP CS Workshop What happens? public class StringTest { public static void main(String[] args) { ArrayList stringList = new ArrayList (); stringList.add("Fran"); stringList.add("Marie"); stringList.add("Joe"); System.out.println(stringList); }

AP CS Workshop WHY???? public class StringTest { public static void main(String[] args) { ArrayList stringList = new ArrayList (); stringList.add("Fran"); stringList.add("Marie"); stringList.add("Joe"); System.out.println(stringList); } [Fran, Marie, Joe] Let's look at the API!

AP CS Workshop The ArrayList is defined using generics. A generic is a method that is recompiled with different types as the need arises. List employeeList = new ArrayList (); Use of generics provides compile-time checking to make sure you are using the correct type Employee emp = employeeList.get(i).getName();

AP CS Workshop ArrayLists contain Objects Numbers are not objects. You can not have an ArrayList of int s or double s or boolean s or char s.

AP CS Workshop Again: An ArrayList is a sequence of objects. Array lists can hold any kind of object. The ArrayList method add adds an Object of specified type. The ArrayList method get returns an Object of specified type. What if we want our ArrayList to hold int s or double s?

AP CS Workshop Filling the ArrayList for (int k = 1; k <= 20; k++) { nbrs.add(new Integer(k)); }

AP CS Workshop Printing the ArrayList for(Integer e : nbrs) { System.out.println(e); } Integer has a toString method that does the right thing.

AP CS Workshop Printing the sum of the integer values in the ArrayList int sum = 0; Integer temp; for (int k = 0; k < nbrs.size(); k++) { temp = nbrs.get(k); sum += temp.intValue(); } System.out.println("sum equals: " + sum);

AP CS Workshop Printing the sum of the integer values in the ArrayList //alternate method using enhanced for and auto //boxing/unboxing sum = 0; for (Integer e:nbrs) { sum += e; } System.out.println("sum equals: " + sum);

AP CS Workshop Java 5.0 introduced Auto-boxing and Auto-unboxing (not tested) –The idea of auto-boxing and auto-unboxing is to make it easier to convert between primitive data types like int, double, and boolean and their equivalent classes, like Integer, Double, and Boolean. It is sometimes frustrating and tedious to have to do such a conversion, especially if results must be converted back to their original form.

AP CS Workshop Autoboxing/unboxing ArrayList numbers = new ArrayList (); numbers.add(14.5); numbers.add(3.4); double sum = numbers.get(0) + numbers.get(1);

AP CS Workshop Auto-boxing and Auto-unboxing Integer/int equality tests int == intOK Integer.equals(Integer)OK Integer == intOK Integer.equals(int)OK Integer.compareTo(int)==0OK

AP CS Workshop Auto-boxing and Auto-unboxing Integer/int equality tests –int.equals(Integer)NO –int.equals(int)NO –Integer == IntegerNO (as test for value equality) –Integer.equals(null)NO

AP CS Workshop Auto-boxing and Auto-unboxing NOT part of the AP CS Testable Subset. Students need to understand –intValue() –doubleValue()

AP CS Workshop ArrayList Applications Finding values, counting, max and min, average, median, mode….. Your students should be able to manipulate all sorts of algorithms for ArrayList s.

AP CS Workshop AP CS A 2007 Question 3a

AP CS Workshop AP CS A 2007 Question 3b

AP CS Workshop AP CS A 2007 Question 3b