Logout

Home Topic 4 Last Next

4.3.11

Construct algorithms using the access methods of a collection.

 

Teaching Note:

LINK Connecting computational thinking and program design.

 

Sample Question:

sdfsdfsf

JSR Notes:

For HL, in Topic 5, we will use the Java Collection implementation ArrayList. ArrayList is an "abstract" class which basically acts like an array, except that it can grow and shrink dynamically. It has the following commonly used methods:

And for the HL take on collections, you can also see the bottom of page 7 and the top of page 8 of the JETS document for a LinkedList Collection. (And actually, beyond just Topic 5, these are more likely to be used in an OOP Option question.)

 

But, for SL, in this Topic 4, it is supposed to be kept to pseudocode, and only a few basic methods, which you can see also on the actual IB document linked below this page in these Topic 4 notes. Here they are:

The methods are:

The basic way you always use them is as follows:

1. loop through adding to the list with addItem( ).

2. before you do anything with a list, make sure it's not empty with if(!isEmpty( ) )

3. make a habit also of using resetNext( ) so that you for sure start looping through your list from the beginning.

4. and then do a combination of checking to see if there is another item, with if(hasNext( ) )

5. and finally, each time through your loop, actually do something with the item, with getNext( )

Here's an example:

4       OurList<String> list = new OurList<String>();
5       list.addItem(new String("hello"));
6       list.addItem(new String("world"));
7       list.addItem(new String("how are you?"));
8       list.resetNext();
9       if (!list.isEmpty()) {
10          while (list.hasNext()) {
11               System.out.println(list.getNext());
12           }
13       } else {
14           System.out.println("List is empty");
15       }

This prints out:

hello
world
how are you?