Arrays versus ArrayList

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.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
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:
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Georgia Institute of Technology Making Text for the Web part 4 Barb Ericson Georgia Institute of Technology March 2006.
Intro-Sound-part21 Introduction to Processing Digital Sounds part 2 Barb Ericson Georgia Institute of Technology Oct 2009.
DataStructures1 Barb Ericson Georgia Tech July 2008.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
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.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
LinkedList Many slides from Horstmann modified by Dr V.
Georgia Institute of Technology Processing Sound Ranges Barb Ericson Georgia Institute of Technology July 2005.
Linked Lists Ellen Walker CPSC 201 Data Structures Hiram College.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
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.
Georgia Institute of Technology Workshop for CS-AP Teachers Data Structures Barb Ericson June 2006.
Georgia Institute of Technology Workshop for CS-AP Teachers Chapter 6 Data Structures.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
Georgia Institute of Technology What is new in Java 5.0 (1.5)? Barb Ericson Georgia Institute of Technology June 2006.
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.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Intro-Sound-Mod10-part31 Introduction to Processing Digital Sounds part 3 while loop, tracing, for loop, parameters Barb Ericson Georgia Institute of Technology.
The ArrayList Data Structure The Most Important Things to Review.
The ArrayList Data Structure Standard Arrays at High Speed!
04-ManipulatingPictures-part21 Manipulating Pictures, Arrays, and Loops part 2 Barb Ericson Georgia Institute of Technology June 2008.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Arrays Chapter 7.
An Array-Based Implementation of the ADT List
Manipulating Pictures, Arrays, and Loops part 2
(like an array on steroids)
Lecture 5 D&D Chapter 6 Arrays and ArrayLists Date.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 7 Part 1 Edited by JJ Shepherd
Georgia Institute of Technology
Queues Queues Queues.
LINKED LISTS CSCD Linked Lists.
Queues 11/9/2018 6:28 PM Queues 11/9/2018 6:28 PM Queues.
Queues 11/16/2018 4:18 AM Queues 11/16/2018 4:18 AM Queues.
Queues 11/16/2018 4:19 AM Queues 11/16/2018 4:19 AM Queues.
Introduction to Processing Digital Sounds part 2
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.
Can store many of the same kind of data together
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Can store many of the same kind of data together
Dynamic Data Structures and Generics
Object Oriented Programming in java
Queues 12/30/2018 9:24 PM Queues 12/30/2018 9:24 PM Queues.
Barb Ericson Georgia Institute of Technology June 2006
Introduction to Processing Digital Sounds part 3
Barb Ericson Georgia Institute of Technology Oct 2005
Manipulating Pictures, Arrays, and Loops
ArrayLists 22-Feb-19.
Can store many of the same kind of data together
Review of Previous Lesson
Suggested self-checks: Section 7.11 #1-11
Manipulating Pictures, Arrays, and Loops
Creating and Modifying Text part 3
Review: libraries and packages
Barb Ericson Georgia Institute of Technology Oct 2005
Workshop for CS-AP Teachers
Arrays and ArrayLists.
Why not just use Arrays? Java ArrayLists.
Presentation transcript:

Arrays versus ArrayList Barb Ericson Georgia Institute of Technology June 2006 Georgia Institute of Technology

Georgia Institute of Technology Learning Goals Understand when you might want to use an array Understand when you might want to use an ArrayList Understand the major differences between Arrays and ArrayLists Understand how to use an Iterator Georgia Institute of Technology

Georgia Institute of Technology Arrays We use arrays to hold many things of the same type Like sample values in a sound Like pixels in a picture We can create arrays of primitive types or objects double[] gradeArray = new double[10]; Picture[] pictureArray; // declare the pictureArray pictureArray = new Picture[5]; // create the array We don't have to name each element We name the array We use indices to access each element (starting at 0) pictureArray[3]; Georgia Institute of Technology

Georgia Institute of Technology Exercise Look at SlideShow.java in examples/SlideShow-Array Change the main to create an array of 7 pictures instead of 5 Add pictures created from files in mediasources Add two new pictures to the slide show at indices 5 and 6 Run the main method Georgia Institute of Technology

Georgia Institute of Technology Benefits of Arrays Holds many items that are related and are of the same type Quick access to any item based on the index of that item Calculates address based on the size of items in the array and distance from the beginning of the array Easy to loop through all items in the array Can use a for-each loop Georgia Institute of Technology

Georgia Institute of Technology Adding to a Full Array To add items into a full array You would need to create a bigger array Copy all the old items before the insert point (if any) Add the new items Copy any remaining old items (ones past the insertion point) 3 | 5 | 7 | 9 3 | 5 | 1 | 2 | 3| 4 | 7 | 9 Add 1, 2, 3, 4 at index 2 Georgia Institute of Technology

Removing From a Full Array You could just copy the items to the left But then you have values at the end that aren't really in the array any more You can zero out primitive types You can set object references to null But what if these are in the array as normal values? You can keep track of the number of valid positions in the array 3 | 1 | 2 | 0 | 4 | 7 | 9 | 0 3 | 5 | 1 | 2 | 0| 4 | 7 | 9 3 | 1 | 2 | 0 | 4| 7 | 9 | 9 Size = 7 Remove 5 Georgia Institute of Technology

Georgia Institute of Technology ArrayList Is a implementation of the List interface Using an array The array will grow or shrink as needed to fit the data You can add items to the List They will be added at the end You can add items at a specified index Other items will be moved to the right to make room You can remove items Items will be moved to the left Georgia Institute of Technology

Differences between Array and ArrayList Get the number of items in Array: arrayName.length (public field) ArrayList: size() (method call) How you declare them Array: Type[] arrayName (use square brackets) ArrayList: List (use interface name as type) How you create them new int[5]; new ArrayList(); Can store Arrays: primitive types or objects ArrayList: only objects (can wrap primitive types) Georgia Institute of Technology

Georgia Institute of Technology Interface Name as Type Any class that implements an interface can be referred to by a variable that is declared with the interface type ArrayList implements the List interface List pictureList = new ArrayList(); This allows you to change your mind in the future about which class to use and only change the code in one place Where you create the list Instead of everywhere you specify the type Georgia Institute of Technology

Georgia Institute of Technology Exercise Modify the SlideShow class in exercises/SlideShow-Array to use an ArrayList Change the field from pictArray to pictList Change the type from an array of pictures (Picture[] to a List) Don't forget to change the documentation, too Change from creating an array to creating an ArrayList Add two new methods that add new Pictures to the slide show One that takes an index to insert at and one that adds it to the end of the list Georgia Institute of Technology

Georgia Institute of Technology Processing a List If you just want to loop through all items in a list Use a for-each loop If you want to remove an item from the list during the loop Use an Iterator and call remove on the Iterator If you want to add an item to the list during the loop Use a ListIterator Georgia Institute of Technology

Processing a List using a While Loop You can also process a list using a while loop But be careful if you remove items from the list during the loop Only increment the index if you didn't remove an item from the list int index = 0; while (index < theList.size()) { obj = theList.get(index); if (obj.test()) theList.remove(index); else index ++; } It is easier to use an iterator Georgia Institute of Technology

Georgia Institute of Technology Iterators Really an interface with these methods: hasNext() returns a boolean value next() returns the next item in the list remove() returns the last returned item from the list Iterator iterator = theList.iterator(); while (iterator.hasNext()) { obj = iterator.next(); if (obj.test()) iterator.remove(); } Georgia Institute of Technology

Georgia Institute of Technology Exercise Modify the SlideShow class in SlideShow-Generic to remove all pictures from the slide show that have a passed string as part of the file name Use an iterator to loop through the pictures in the list And use indexOf method to look for the passed search string If you find the search string remove the last item from the iterator Georgia Institute of Technology

Georgia Institute of Technology ListIterator An interface Inherits from the List interface Allows you to process a list from the front or back hasNext() or hasPrevious() to test for elements next() or previous() to get an element Can add or set items in the list add(E o) before next or set(E o) to replace last Georgia Institute of Technology

Georgia Institute of Technology Summary Use arrays to hold groups of data of the same type Especially when the size of the data is known and doesn't change Use ArrayList to hold objects in order Especially when you don't know how much data to expect And there may be some additions or deletions There isn't too much data Use iterators to process a list Especially if you want to remove or add items during the loop Georgia Institute of Technology