Collections - Introduction to Programming in Java - Lecture Notes | CSIS 110, Study notes of Javascript programming

Material Type: Notes; Professor: Hanks; Class: Intro to Programming in Java; Subject: Computer Science Info Systems; University: Fort Lewis College; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 08/05/2009

koofers-user-s6c
koofers-user-s6c 🇺🇸

10 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CSIS 110 – Lecture 10
Homework
Due Sept 26. Show students my version.
Ask if there are any questions?
Coding Standard!
Reading: Chapter 4: 4.1 through 4.8 (you can skip 4.7.2 and 4.7.3)
Last time: Collections
What’s a collection?
- A group of variables that can be referenced using a single name
ArrayList
- A collection provided in the Java library
- We can create an ArrayList object, and then use its methods to insert and
access elements.
- Need to tell Java where it is – use import statement
Instance Variable in class Notebook:
ArrayList<String> notes;
Create a new ArrayList object in the constructor:
notes = new ArrayList<String>();
Remind students that this is written using Java 1.5 – slightly different than the book,
which uses Java 1.4.
Some ArrayList methods:
add – store an object in the list
size – return the number of objects in the list
get – get an object from the list (but don’t remove it)
Ask students how to invoke methods on the above object (notes).
Properties of ArrayLists:
Variable size – an ArrayList gets bigger if necessary as we add more items
It keeps track of the number of items it currently contains
pf3

Partial preview of the text

Download Collections - Introduction to Programming in Java - Lecture Notes | CSIS 110 and more Study notes Javascript programming in PDF only on Docsity!

CSIS 110 – Lecture 10

Homework Due Sept 26. Show students my version. Ask if there are any questions? Coding Standard! Reading: Chapter 4: 4.1 through 4.8 (you can skip 4.7.2 and 4.7.3) Last time: Collections What’s a collection?

  • A group of variables that can be referenced using a single name ArrayList
  • A collection provided in the Java library
  • We can create an ArrayList object, and then use its methods to insert and access elements.
  • Need to tell Java where it is – use import statement Instance Variable in class Notebook: ArrayList notes; Create a new ArrayList object in the constructor: notes = new ArrayList(); Remind students that this is written using Java 1.5 – slightly different than the book, which uses Java 1.4. Some ArrayList methods: add – store an object in the list size – return the number of objects in the list get – get an object from the list (but don’t remove it) Ask students how to invoke methods on the above object (notes). Properties of ArrayLists:  Variable size – an ArrayList gets bigger if necessary as we add more items  It keeps track of the number of items it currently contains

 It maintains the order of the items. We can retrieve the items in the same order as we added them.  It can be used to store objects of any type (but not primitives!) Look back at the Notebook source – it looks pretty simple, as it contains only 1 instance variable. All the difficult work is done in the ArrayList – this is one of the great advantages of using library classes: someone else has taken the time to develop something useful, and we can use it. Discuss Notebook constructor, storeNote, and numberOfNotes methods. Point out that the Notebook doesn’t keep track of the number of notes – it lets the ArrayList do that instead. Numbering with collections Each item in a collection can be accessed individually using its index , or position in the list. What was something important about indexing? Indices are numbered from 0 to listSize -

Look at the showNote method – most of the code is there to make sure that you don’t try to access the ArrayList outside of its valid range. Accessing all of the elements in an ArrayList – The while statement Ok, so we can create a Notebook, add Notes to it, and display the individual Notes. What would I need to do to access display all of the Notes in the Notebook? We could just call showNote(0), showNote(1), showNote(2), … What’s wrong with this approach? We need some sort of repetitive (iterative) operation: a loop Java provides some looping statements – one of these is the while statement. while ( boolean-expr ) { statements; } The while statement works this way:

  1. Evaluate the boolean expression. If it is true go to step 2. Otherwise, go to step 4.
  2. Execute the statements in the while-loop body.
  3. Go to step 1
  4. Execute the statement following the while loop.