Lecture 8 Understanding Collections contd Textbook Chapter 4
Lecture 8: Understanding Collections (contd) • Textbook: Chapter 4
While loop pseudo code General form of a while loop while keyword Boolean test while(loop condition) { loop body } Statements to be repeated
For loop pseudo-code General form of a for loop for(initialization; condition; post-body action) { statements to be repeated } Equivalent in while-loop form initialization; while(condition) { statements to be repeated post-body action }
Do -While loop pseudo code General form of a while loop while keyword Boolean test do { loop body } while(loop condition) Statements to be repeated
Object Structures with Collections • There 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. FDetails 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 Array. List 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.
Anonymous Objects • Anonymous object is an object without name. It is passed directly to the method that uses it. public void add. Book(String name) { books. add(new Book(name)); } • We created a new Book object • We passed the new object to the Array. List’s add method.
Iterators • Examining every item in a collection is a very common activity. • The Array. List class supplies an iterator method that supplies an Iterator object that allows us to iterate over the collection • We must add another import statement to use the iterator object – import java. util. Iterator;
Casting • Casting is an important feature of Java Book b = (Book) it. next(); • A cast consists of the name of a type written alone between a pair of parentheses • Casting is commonly seen when retrieving objects from a collection • This is required because collections can store any type of object • Casting makes it clear what type of object it is
Fixed-size collections • Sometimes the maximum collection size can be pre-determined. • Programming languages usually offer a special fixed-size collection type: an array. • Java arrays can store objects or primitivetype values. • Arrays use a special syntax.
Declaring and Creating array variables Consider a scenario where you need to create an array of 24 integers. Step 1: Declare an array of size 24 Step 2: Create an array of size 24
Actual Code public class some. Class { private int[] hour. Counts; public some. Class() { hour. Counts = new int[24]; }. . . }
The hour. Counts array
Using an array • Square-bracket notation is used to access an array element: hour. Counts[. . . ] • Elements are used like ordinary variables. – On the left of an assignment: • hour. Counts[hour] =. . . ; – In an expression: • adjusted = hour. Counts[hour] – 3; • hour. Counts[hour]++;
- Slides: 15