Copyright 2008 by Pearson Education Building Java Programs ArrayList Reading: 10.1.

Slides:



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

The ArrayList Class and the enum Keyword
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;
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.
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Building Java Programs
Copyright 2008 by Pearson Education Building Java Programs Chapter 7 Lecture 27: More on ArrayList, Reference Semantics Command Line Arguments reading:
Arrays And ArrayLists - S. Kelly-Bootle
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.
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.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
ArrayList, Multidimensional Arrays
Chapter 11 Arrays Continued
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
Copyright 2008 by Pearson Education Building Java Programs Chapter 10 Lecture 10-1: ArrayList reading: 10.1.
23-Oct-15 Abstract Data Types. 2 Data types A data type is characterized by: a set of values a data representation, which is common to all these values,
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Computer Science 209 Software Development Java Collections.
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 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
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.
CompSci 100E Functions (Static Methods)  Java function.  Takes zero or more input arguments.  Returns one output value.  Applications.  Scientists.
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.
CSE 143 Lecture 2 ArrayList reading: 10.1 slides created by Marty Stepp
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
Copyright 2010 by Pearson Education Building Java Programs Chapter 10 Lecture 21: ArrayList reading: 10.1.
Copyright 2010 by Pearson Education Building Java Programs Chapter 10, 11 Lecture 22: 143 Preview optional reading: 10.1,
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
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.
CSE 1201 Object Oriented Programming ArrayList 1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
The ArrayList Data Structure The Most Important Things to Review.
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.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 9: Arrays; Revision Session.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 6 GEORGE KOUTSOGIANNAKIS Copyright: 2016 Illinois Institute of Technology/George Koutsogiannakis 1.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
An Array-Based Implementation of the ADT List
(like an array on steroids)
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
COP 3503 FALL 2012 Shayan Javed Lecture 8
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Welcome to CSE 143!.
TCSS 143, Autumn 2004 Lecture Notes
ArrayLists.
Programming in Java Lecture 11: ArrayList
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.
Copyright ©2012 by Pearson Education, Inc. All rights reserved
CSE 143 Lecture 4 ArrayList Reading: 10.1.
ArrayLists.
Words exercise Write code to read a file and display its words in reverse order. A solution that uses an array: String[] allWords = new String[1000]; int.
ArrayList Collections.
Chapter 10 ArrayList reading: 10.1
Welcome to CSE 143!.
Grouped Data Arrays, and Array Lists.
ArrayLists 22-Feb-19.
ArrayLists.
slides created by Marty Stepp
ArrayLists 27-Apr-19.
TCSS 143, Autumn 2004 Lecture Notes
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Presentation transcript:

Copyright 2008 by Pearson Education Building Java Programs ArrayList Reading: 10.1

Copyright 2008 by Pearson Education 2 Easier than Arrays Java’s arrays are really useful for many programming tasks And they can hold elements of any type But you must choose the size when you make the array Cannot “grow or shrink” as the program runs Could make a new array Or use the ArrayList library, which does this for you Complicated internal behavior, simple external The first library we have seen that “works for any type” But you must specify the type with some special syntax

Copyright 2008 by Pearson Education 3 ArrayList basics import java.util.*; ArrayList name = new ArrayList (); “Angle-bracket syntax” the new thing: type of elements Initially holds 0 elements () because we’re calling a 0-argument constructor new returns a new ArrayList object, of course. Use its methods to access and mutate the contents…

Copyright 2008 by Pearson Education 4 ArrayList methods The primary methods for manipulating an ArrayList : add(E value) appends value at end of list add(int index, E value) inserts given value at given index, shifting subsequent values right clear() removes all elements of the list get(int index) returns the value at given index remove(int index) removes and returns value at given index, shifting subsequent values left set(int index, E value) replaces value at given index with given value size() returns the number of elements in list Notice types for add and set depend on type of ArrayList Won’t learn in 142 how to define our own classes that do this But we can use ArrayList, written by others

Copyright 2008 by Pearson Education 5 Mini-Exercise Make a new ArrayList named creatures and add the strings "octopus" and "squid" to it. Cheat sheet: ArrayList name = new ArrayList (); The primary methods for manipulating an ArrayList : add(E value) appends value at end of list add(int index, E value) inserts given value at given index, shifting subsequent values right clear() removes all elements of the list get(int index) returns the value at given index remove(int index) removes and returns value at given index, shifting subsequent values left set(int index, E value) replaces value at given index with given value size() returns the number of elements in list

Copyright 2008 by Pearson Education 6 Mini-Exercise -solution Make a new ArrayList named creatures and add the strings "octopus" and "squid" to it. ArrayList creatures = new ArrayList (); creatures.add("octopus"); creatures.add("squid");

Copyright 2008 by Pearson Education 7 An annoying limitation Can make an ArrayList for any type of object you want But int, double, boolean, and char are not types of objects ( String is) You can make: ArrayList ArrayList Java will automatically convert between numbers/booleans and objects of these wrapper classes for you A convenient, but fairly confusing and new feature to Java For ArrayList, all you need to know is that you have to write Integer, etc. in the angle brackets, not int, etc.

Copyright 2008 by Pearson Education 8 Practice with ArrayList (See separate handout for several “exam-style” ArrayList questions that show how an ArrayList can be used) Particularly notice what is easier than with arrays: add and remove “in the middle” having the size change Exercise on your own: Rewrite one or both of our sorting algorithms using ArrayList

Copyright 2008 by Pearson Education 9 Recall: earthquake problem Given a file of cities' (x, y) coordinates, which begins with the number of cities: Write a program to draw the cities on a DrawingPanel, then color the cities red that are within the radius of effect of the earthquake: Epicenter x/y? Radius of effect? 75

Copyright 2008 by Pearson Education 10 We made it a little too easy Given a file of cities' (x, y) coordinates, which begins with the number of cities: What if we didn’t know the number of cities in advance? Yes, “use a while loop”, but first we have to make the array!

Copyright 2008 by Pearson Education 11 Solutions 1. Read the file twice First time just to count how many cities Okay, but won’t work if the problem reads cities from the console until the user types, “STOP” 2. Resize the array as necessary A common idiom worth knowing Doubling each time is a very elegant policy Never waste more than half the space And you won’t make very many arrays (2 25 > 32 million) 3. Use an ArrayList Just call add each time Probably the ArrayList library is using something like resizing, but that’s somebody else’s problem Don’t reinvent the wheel