Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 8: Understanding Collections Textbook: Chapter 4.

Similar presentations


Presentation on theme: "Chapter 8: Understanding Collections Textbook: Chapter 4."— Presentation transcript:

1 Chapter 8: Understanding Collections Textbook: Chapter 4

2 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.

3 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

4 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.

5 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.

6 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.

7 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 }

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

9 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 }

10 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

11 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.


Download ppt "Chapter 8: Understanding Collections Textbook: Chapter 4."

Similar presentations


Ads by Google