Chapter 8: Understanding Collections Textbook: Chapter 4.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Programming Logic and Design, Third Edition Comprehensive
Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Using Collections. Review of Collections Using an ArrayList It increases its capacity as necessary. It keeps a private count ( size() accessor). It keeps.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Grouping Objects 1 Introduction to Collections.
Grouping objects Collections and iterators. 04/11/2004Lecture 4: Grouping Objects2 Main concepts to be covered Collections Loops Iterators Arrays.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Modul 3 Collections af objekter Arraylist Collections Objektorienteret design og Java. 4.0.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
REPETITION CITS1001. Scope of this lecture Repetition for loops while loops 2.
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Grouping objects Arrays, Collections and Iterators 1.0.
IMPLEMENTING ARRAYLIST – Part 2 COMP 103. RECAP  Abstract Classes – overview, details in 2 nd year  Implementing the ArrayList: size(), get(), set()
6 Chapter 61 Looping Programming Logic and Design, Second Edition, Comprehensive 6.
Data structures Abstract data types Java classes for Data structures and ADTs.
Grouping objects Introduction to collections 5.0.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
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.
JAVA COLLECTIONS M. TAIMOOR KHAN (ADAPTED FROM SWINBURNE NOTES)
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
COMPUTER PROGRAMMING 2 ArrayLists. Objective/Essential Standard Essential Standard 3.00Apply Advanced Properties of Arrays Essential Indicator 3.02 Apply.
Grouping objects Arrays. 2 Fixed-size collections The maximum collection size may be pre-determined with an upper limit Array is an fixed-size collection.
Grouping objects Introduction to collections 5.0.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
 2015, Marcus Biel, Linked List Data Structure Marcus Biel, Software Craftsman
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. for-each PROS easy to use access to ALL items one-by-one ability to change the state.
for-each PROS CONS easy to use access to ALL items one-by-one
Objects First with Java CITS1001 week 4
Databases Chapter 9 Asfia Rahman.
Objects First with Java Introduction to collections
Control Structures.
Loop Structures.
ARRAYLIST AND VECTOR.
JavaScript: Functions.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
While loops The while loop executes the statement over and over as long as the boolean expression is true. The expression is evaluated first, so the statement.
Arrays, For loop While loop Do while loop
Looping and Repetition
Lesson #5 Repetition and Loops.
Arrays and Collections
Object Oriented Programming in java
Classes and Objects.
Cs212: Data Structures Computer Science Department Lecture 7: Queues.
3.1 Iteration Loops For … To … Next 18/01/2019.
Collections and iterators
A LESSON IN LOOPING What is a loop?
Dr. Sampath Jayarathna Cal Poly Pomona
Data Structures & Algorithms
PROGRAM FLOWCHART Iteration Statements.
Review: libraries and packages
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Collections and iterators
Arrays.
Looping and Repetition
Lecture Set 9 Arrays, Collections, and Repetition
Presentation transcript:

Chapter 8: Understanding Collections Textbook: Chapter 4

Class Exercise Books and Library Books: –Information: A book should have name, author, year of publication, publisher associated with it, if its available or checked out, how many copies of the book there are, how many checked out. –Operations/Method: Should be able to tell the name of the book, publisher, …access stored information, Library: –Maintain a list of books, sometimes there can be more than one copy of books, so should be able to handle more than one copy of books, browse through books, and add new books, remove books when they are lost or damaged.

Flexible Sized Collections We could create a class with a lot of fixed fields, but in general this will not work It is typical that the number of items to be stored will vary over time When writing a program, we often need to be able to group objects into collections

Object Structures with Collections There are at least three important features –It is able to increase its internal capacity as required: as more items are added, it simply makes enough room for them. –It keeps its own private count of how many items it is currently storing. Its size method returns the number of objects currently stored –It maintains the order of items you insert into it. You can later retrieve them in the same order.  Details of how all this is done is hidden.

Numbering within Collections It is necessary to use values starting at zero. Items are stored in the ArrayList starting a number position zero The position is known as the index First is given index 0, the next is index 1, … It is a common mistake to try to access a collection outside the valid indices.

Removing Elements When a user wants to remove a book, we can invoke the remove method of the notebook object One complication of the removal process is that it can change the index values at which books are stored If a note with a low index value is removed, then the collection moves all notes forward to fill the hole. Furthermore, it is possible to insert into other than at the end.

The while loop A while loop is one way to perform a set of actions repeatedly, without having to write those actions more than once. Here is a general format while ( loop condition ) { loop body //statements you want to //repeat }

While loop pseudo code while(loop condition) { loop body } Boolean test while keyword Statements to be repeated General form of a while loop

For loop pseudo-code for(initialization; condition; post-body action) { statements to be repeated } General form of a for loop Equivalent in while-loop form initialization; while(condition) { statements to be repeated post-body action }

Do -While loop pseudo code do { loop body } while(loop condition) Boolean test while keyword Statements to be repeated General form of a while loop

Loops vs. If Do not confuse a while loop with an if. Although they look similar, they operate in very different ways The biggest difference is that once the body of the loop has been executed for the first time, we go back to the test again to see if the body should be executed.