BUILDING JAVA PROGRAMS CHAPTER 10.1 ARRAY LISTS. 22 OBJECTIVES! Use ArrayList to construct and analyze lists of arbitrary length.

Slides:



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

Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
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;
Generics, Lists, Interfaces
Java Programming Strings Chapter 7.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
Recitation 3/27/2009 CS 180 Department of Computer Science, Purdue University.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS180 Recitation 25th/26th October, 2007 Department of Computer Science, Purdue University.
Building Java Programs
1 CSE 143 Lecture 1: ArrayList reading: Welcome to CSE 143! I'm Allison Obourn
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 10: ArrayList.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic concepts and uses of arrays ❏ To be able to define C.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
A review session 2013-T2 Lecture 16 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Marcus Frean.
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.
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.
1 BUILDING JAVA PROGRAMS CHAPTER 7.1 ARRAY BASICS.
BUILDING JAVA PROGRAMS CHAPTER 7 Arrays. Exam #2: Chapters 1-6 Thursday Dec. 4th.
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
VB and C# Programming Basics. Overview Basic operations String processing Date processing Control structures Functions and subroutines.
Building Java Programs Chapter 11 Lecture 11-1: Sets and Maps reading:
Chapter 18 Java Collections Framework
1 Chapter 20 Lists, Stacks, Queues Lecture 7 Dr. Musab Zghoul برمجة هيكلية.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
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.
JAVA COLLECTIONS M. TAIMOOR KHAN (ADAPTED FROM SWINBURNE NOTES)
CSE 143 Lecture 2 ArrayList reading: 10.1 slides created by Marty Stepp
Copyright 2010 by Pearson Education Building Java Programs Chapter 10 Lecture 21: ArrayList reading: 10.1.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
ITI 1120 Lab #5 Contributors: S. Boyd, R. Plesa, A. Felty, D. Inkpen, A. Williams, D. Amyot.
1 Today’s Material List ADT –Definition List ADT Implementation: LinkedList.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
1 BUILDING JAVA PROGRAMS CHAPTER 7.2 ARRAY TRAVERSAL ALGORITHMS.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Chapter 8 Slides from GaddisText Arrays of more than 1 dimension.
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.
Lecture 9:FXML and Useful Java Collections Michael Hsu CSULA.
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
Lecture 5 of Computer Science II
(like an array on steroids)
Containers and Lists CIS 40 – Introduction to Programming in Python
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Building Java Programs
The relational operators
CS 106A, Lecture 19 ArrayLists
Lecture 2: Implementing ArrayIntList reading:
Building Java Programs
© Akhilesh Bajaj, All rights reserved.
Building Java Programs
ArrayLists.
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
Chapter 10 ArrayList reading: 10.1
ArrayLists.
CSE 143 Lecture 2 Collections and ArrayIntList
Lecture 2: Implementing ArrayIntList reading:
CSE 143 Lecture 3 Implementing ArrayIntList reading:
Building Java Programs
CSE 143 Lecture 4 Implementing ArrayIntList reading:
slides created by Ethan Apter and Marty Stepp
Arrays and ArrayLists.
Why not just use Arrays? Java ArrayLists.
Presentation transcript:

BUILDING JAVA PROGRAMS CHAPTER 10.1 ARRAY LISTS

22 OBJECTIVES! Use ArrayList to construct and analyze lists of arbitrary length.

33 IN YOUR NOTEBOOKS… 1. Last time we computed average word length, we had to first ask the user for an integer before we could start inputting words… why? What did we need the integer for? 2. Can you think of a way to get an arbitrarily long list of words from the user without first asking for an integer?

44 ARRAYLIST Similar to arrays, but you don’t need to declare size up front… just add/remove as much as you like! It overrides toString() in a useful way You can use the same looping techniques you’re already good at

55 ARRAYLIST EXAMPLE ArrayList lies = new ArrayList (); lies.add("Nobody took the cookies from the cookie jar!"); lies.add("Programmers have more fun."); lies.add("Vegemite tastes great!"); // but I'm pretty sure the 2nd one's true... lies.remove(1); // What does this do? for (String lie : lies) { System.out.println("Lie: " + lie); } // it’s our old friend the "for-each"

66 ARRAYLIST How do you add a new value to the end of the ArrayList ? How do you get the number of elements in an ArrayList ? What if you want to get the element at a particular index? ArrayList list = new ArrayList (); list.add("another value"); int length = list.size(); String myElement = list.get(2);

77 ARRAYLIST How would you delete the element at a particular index? And last but not least… what if you want to insert and element at a particular index? You can also clear the whole array list with list.clear() and change the value at a location with list.set(index, value) These can all be found on page 654 in your text! list.remove(2); list.add(2, "sweeeeeet!");

88 FINDING VALUES… There are some other helpful methods you can use for searching in an ArrayList : contains(), indexOf() and lastIndexOf(). These are like the similarly named methods on the String class.

99 ARRAYLIST AND FOR-EACH Chapter 10.1 also talks about using a for-each loop with an ArrayList. What was the thing you have to watch out for when using a for- each loop with an ArrayList ? You can use the for-each loop as long as you’re only “visiting” each value. You can not change the ArrayList (by adding or removing a value) while you’re in the loop. If you attempt to modify the list in a for-each loop, Java will throw a somewhat helpfully-named ConcurrentModificationException.

10 CODE CODE CODE! Write a method called getLines which reads lines from the user until !go is entered. It should return an ArrayList of all the lines other than !go. Write a method called averageLineLength which takes an ArrayList and then prints each line and then the average line length. Try using the “for-each” loop. Make a main method that uses getLines and averageLineLength. Write a method called removeDuplicates which takes an ArrayList and uses a nested for loops to remove any duplicate strings. Then make your main method print the average line length before and after removing duplicates.

11 WHAT DID WE COVER TODAY? Use ArrayList to construct and analyze lists of arbitrary length.