Copyright 2010 by Pearson Education Building Java Programs Chapter 10 Lecture 21: ArrayList reading: 10.1.

Slides:



Advertisements
Similar presentations
Building Java Programs Chapter 10
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;
Building Java Programs Chapter 14
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Building Java Programs
1 CSE 143 Lecture 1: ArrayList reading: Welcome to CSE 143! I'm Allison Obourn
CSE 143 Lecture 7 Stacks and Queues reading: Stuart Reges notes on website slides created by Marty Stepp
Building Java Programs
CSE 143 Lecture 5 Stacks and Queues slides created by Marty Stepp
Teach.NET Workshop Series Track 4: AP Computer Science with.NET and J#
ARRAYLIST.. Hazen High School. Vocabulary to Know ArrayList Generic Class ArrayList Operations ArrayList Methods ArrayList Searching For-Each Wrapper.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
AP Comp Sci A Chapter 12 - Arrays. Ch 12 Goals: Goals: Declare and create arrays Declare and create arrays Access elements in arrays Access elements in.
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.
1 Chapter 20 Lists, Stacks, Queues Lecture 7 Dr. Musab Zghoul برمجة هيكلية.
Copyright 2008 by Pearson Education Building Java Programs ArrayList Reading: 10.1.
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.
Stacks and Queues. 2 3 Runtime Efficiency efficiency: measure of computing resources used by code. can be relative to speed (time), memory (space), etc.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
CSE 143 Lecture 2 ArrayList reading: 10.1 slides created by Marty Stepp
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 4 Exceptions and ArrayList slides created by Marty Stepp
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.
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
Building Java Programs Chapter 10 ArrayList Copyright (c) Pearson All rights reserved.
Stacks and Queues. 2 Abstract Data Types (ADTs) abstract data type (ADT): A specification of a collection of data and the operations that can be performed.
slides created by Marty Stepp
Collections.
Collections.
Building Java Programs Chapter 14
Building Java Programs
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Welcome to CSE 143!.
Building Java Programs
Building Java Programs Chapter 10
CS 106A, Lecture 19 ArrayLists
TCSS 143, Autumn 2004 Lecture Notes
Lecture 2: Implementing ArrayIntList reading:
ArrayLists.
CSE 143 Lecture 4 ArrayList Reading: 10.1.
ArrayLists.
Building Java Programs Chapter 10
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.
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Building Java Programs
Topic 33 ArrayList.
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Chapter 10 ArrayList reading: 10.1
CSE 143 Lecture 2 Collections and ArrayIntList
Welcome to CSE 143!.
Lecture 1: ArrayList reading: 10.1
Welcome to CSE 143! Go to pollev.com/cse143.
Building Java Programs
Lecture 13: ArrayLists AP Computer Science Principles
CSE 143 Lecture 4 Implementing ArrayIntList
CSE 143 Lecture 2 More ArrayList; classes and objects
Lecture 16: Arraylist Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
ArrayLists.
Parsing JSON, Using Libraries, Java Collections, Generics
slides created by Marty Stepp
Building Java Programs
slides created by Alyssa Harding
slides created by Marty Stepp
Arrays and ArrayLists.
CSE 143 Lecture 1 Arrays (review); ArrayList reading: 10.1
Presentation transcript:

Copyright 2010 by Pearson Education Building Java Programs Chapter 10 Lecture 21: ArrayList reading: 10.1

Copyright 2010 by Pearson Education End of CSE142: Where to go from here

Copyright 2010 by Pearson Education 3 Courses at UW CSE 143 – Computer Programming II More object-oriented programming Basic data structures (Stacks, Queues, Trees, etc.) Recursive Algorithms CSE 154 – Web programming HTML, CSS, Javascript, PHP, MySQL CSE 373 – Data Structures and Algorithms After CSE 143 More advanced data structures and algorithms

Copyright 2010 by Pearson Education 4 Some Programs at UW CSE (Computer Science and Engineering) List of research areas: iSchool (Information School) “Information schools are interested in the relationship between information, technology, and people.” HCDE (Human Centered Design and Engineering) “Study Human Computer Interaction (HCI), User Experience (UX) Research and Design, Interaction Design and Prototyping, and Sociotechnical Systems” Engineering (Mechanical, Electrical, etc.) Sciences (Physics, Biology, etc.) Math (Statistics, Discrete Math, etc.)

Copyright 2010 by Pearson Education 5 Online Tutorials Web programming w3schools: Try HTML, javascript, css, jQuery Code Academy Try Python or Ruby in “Language Skills” Khan Academy Try “Intro to SQL” Many more…

Copyright 2010 by Pearson Education 6 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 wordCount = 0; Scanner input = new Scanner(new File("words.txt")); while (input.hasNext()) { String word = input.next(); allWords[wordCount] = word; wordCount++; } for(int i = allWords.length - 1; i >= 0; i++) { System.out.print(allwords[i] + " "); } What's wrong with this?

Copyright 2010 by Pearson Education 7 Recall: Arrays (7.1) array: object that stores many values of the same type. element:One value in an array. index:0-based integer to access an element from an array. length:Number of elements in the array. index value element 0element 4element 9 length = 10

Copyright 2010 by Pearson Education 8 Array Limitations Fixed-size Adding or removing from middle is hard Not much built-in functionality (need Arrays class)

Copyright 2010 by Pearson Education 9 List Abstraction Like an array that resizes to fit its contents. When a list is created, it is initially empty. [] Use add methods to add to different locations in list [hello, ABC, goodbye, okay] The list object keeps track of the element values that have been added to it, their order, indexes, and its total size. You can add, remove, get, set,... any index at any time.

Copyright 2010 by Pearson Education 10 Collections and lists collection: an object that stores data ("elements") import java.util.*; // to use Java's collections list: a collection of elements with 0-based indexes elements can be added to the front, back, or elsewhere a list has a size (number of elements that have been added) in Java, a list can be represented as an ArrayList object

Copyright 2010 by Pearson Education 11 Type parameters (generics) ArrayList name = new ArrayList (); When constructing an ArrayList, you must specify the type of its elements in This is called a type parameter ; ArrayList is a generic class. Allows the ArrayList class to store lists of different types. Arrays use a similar idea with Type [] ArrayList names = new ArrayList (); names.add("Marty Stepp"); names.add("Stuart Reges");

Copyright 2010 by Pearson Education 12 ArrayList methods (10.1)* add( value ) appends value at end of list add( index, value ) inserts given value just before the given index, shifting subsequent values to the right clear() removes all elements of the list indexOf( value ) returns first index where given value is found in list (-1 if not found) get( index ) returns the value at given index remove( index ) removes/returns value at given index, shifting subsequent values to the left set( index, value ) replaces value at given index with given value size() returns the number of elements in list toString() returns a string representation of the list such as "[3, 42, -7, 15]"

Copyright 2010 by Pearson Education 13 ArrayList vs. array construction String[] names = new String[5]; ArrayList list = new ArrayList (); storing a value names[0] = "Jessica"; list.add("Jessica"); retrieving a value String s = names[0]; String s = list.get(0);

Copyright 2010 by Pearson Education 14 ArrayList vs. array String[] names = new String[5]; // construct names[0] = "Jessica"; // store String s = names[0]; // retrieve for (int i = 0; i < names.length; i++) { if (names[i].startsWith("B")) {... } } // iterate ArrayList list = new ArrayList (); list.add("Jessica"); // store String s = list.get(0); // retrieve for (int i = 0; i < list.size(); i++) { if (list.get(i).startsWith("B")) {... } } // iterate

Copyright 2010 by Pearson Education 15 ArrayList as param/return public static void name (ArrayList name ) {// param public static ArrayList name ( params ) // return Example: // Returns count of plural words in the given list. public static int countPlural( ArrayList list ) { int count = 0; for (int i = 0; i < list.size(); i++) { String str = list.get(i); if (str.endsWith("s")) { count++; } return count; }

Copyright 2010 by Pearson Education 16 Words exercise, revisited Write a program that reads a file and displays the words of that file as a list. Then display the words in reverse order. Then display them with all plurals (ending in "s") capitalized. Then display them with all plural words removed.

Copyright 2010 by Pearson Education 17 Exercise solution (partial) ArrayList allWords = new ArrayList (); Scanner input = new Scanner(new File("words.txt")); while (input.hasNext()) { String word = input.next(); allWords.add(word); } // display in reverse order for (int i = allWords.size() - 1; i >= 0; i--) { System.out.println(allWords.get(i)); } // remove all plural words for (int i = 0; i < allWords.size(); i++) { String word = allWords.get(i); if (word.endsWith("s")) { allWords.remove(i); i--; }

Copyright 2010 by Pearson Education 18 ArrayList of primitives? The type you specify when creating an ArrayList must be an object type; it cannot be a primitive type. // illegal -- int cannot be a type parameter ArrayList list = new ArrayList (); But we can still use ArrayList with primitive types by using special classes called wrapper classes in their place. // creates a list of ints ArrayList list = new ArrayList ();

Copyright 2010 by Pearson Education 19 Wrapper classes A wrapper is an object whose sole purpose is to hold a primitive value. Once you construct the list, use it with primitives as normal: ArrayList grades = new ArrayList (); grades.add(3.2); grades.add(2.7);... double myGrade = grades.get(0); Primitive Type Wrapper Type int Integer double Double char Character boolean Boolean