Lecture 22, CS 302-6, October 24

  1. Reminders
    1. Exam 1 – we’ll discuss Wednesday
    2. Program 2 – October 28

                                                               i.      10pm today-pairs must be registered

    1. Program 1 – a couple of general comments

                                                               i.      Line length

1.      In eclipse: Window -> Preferences -> General -> Editors -> Text Editors -> Show print margin (80)

                                                             ii.      Constants

1.      example – menu choices

a.       final int FEED=1;

b.      if(choice==FEED){}

  1. Last time (bonus class!)
    1. Recursion

                                                               i.      You will not be held responsible for recursion on tests etc

  1. ArrayList motivation
    1. Think about Program 2

                                                               i.      Consider a map

0 1 0 2

0 0 2 0

0 0 0 1

0 0 0 0

                                                             ii.      Also, declare/initialize routes to be the size of the map

                                                            iii.      Think of the roads as having arrows indicating direction – you can only go one way

                                                           iv.      think about the routes that we can get from above

1.      [1, 2, 3, 4]

2.      [1, 4]

                                                             v.      Now, imagine we had these routes:

0 1 0 2

0 0 2 0

0 1 0 1

0 0 0 0

                                                           vi.      Think about what could happen to our routes array

1.      We might need to put more elements in it than it can hold, since we might get stuck in a ‘loop’ in our map

                                                          vii.      Ways around this:

1.      Start array for routes off really big, partially fill it

a.       How do we know how big to make it?  What if it’s still too small…

2.      Increase size if we are going to exceed the current size – copy array into new array, etc…

3.      But, this is tricky, and slow

  1. ArrayList basics
    1. What is an arraylist?  Why use one?

                                                               i.      It’s kind of like an array, with these differences:

1.      Don’t need to worry about initial size!

2.      It can grow

a.       Consider the antRoutes example above

3.      It can shrink

a.       Why is this important?  Well, when you declare an array, it holds that space in memory, so you can’t use it for anything else.  So, if it’s possible that it might hold 1,000,000 things, you need to declare it that size.  ArrayLists can shrink as appropriate.

4.      Finally – it’s easier!  Fewer things to worry about

a.       aka ArrayIndexOutOfBounds exceptions

  1. ArrayList declaration/initialization
    1. We’ll consider an array list of strings.

                                                               i.      Import ArrayList package

1.      import java.util.ArrayList;

                                                             ii.      Declare

1.      ArrayList<String> words;

                                                            iii.      Initialize

1.      words =new ArrayList<String>();

                                                           iv.      General form:

1.      ArrayList<Type Parameter> name = new ArrayList<Type Parameter>();

                                                             v.      What is the type parameter?

1.      Tells the array list what kind of object to hold

2.      ***Note – you cannot pass primitive types as Type Parameters

                                                           vi.      If you want an arraylist of primitive types, instead pass wrapper classes for those primitives

1.      ArrayList<Integer>

2.      ArrayList<Double>

3.      ArrayList<Character>

                                                          vii.      What do we call a class that can take a type parameter like this?

1.      Generic class

  1. ArrayList use
    1. First, make sure you’ve initialized it before using!

                                                               i.      words=new ArrayList<String>();

                                                             ii.      Note the () – this calls the class ‘constructor’.  We’ll explain this soon.  Kind of like rand=new Random();

                                                            iii.      If you don’t initialize first - error

    1. When you initialize an ArrayList, it has size 0
    2. How do you add elements?

                                                               i.      words.add(“Rob”); words.add(“Bill”); words.add(“Mitch”);

                                                             ii.      Generally-

1.      name.add(element);

                                                            iii.      Note – this adds to the end of the array list.  We’ll consider adding to the middle in a moment

    1. How do you get the ArrayList size

                                                               i.      Number of elements in the ArrayList

                                                             ii.      words.size();

                                                            iii.      Generally –

1.      name.size();

    1. How do you get one element from the ArrayList?

                                                               i.      Say I wanted to get my name from the above ArrayList

1.      words.get(0);

                                                             ii.      Note – indexing starts at 0, just like arrays and string characters

                                                            iii.      Generally –

1.      name.get(index);

                                                           iv.      One error to watch out for –

1.      words.get(words.size())

    1. How do you overwrite elements in an ArrayList?

                                                               i.      words.set(0, “Robert”);

                                                             ii.      Generally:

1.      name.set(index,new value);

                                                            iii.      Note – this is just for overwriting existing values, not for adding new ones

    1. How do you add new elements in a given position in an ArrayList?

                                                               i.      words.add(1, “Christina”);

                                                             ii.      Generally -

1.      name.add(index,new value);

                                                            iii.      This inserts the new value at the given index, and ‘pushes everything back’ one spot.

                                                           iv.      Thus, it increases the size of the arraylist by 1

    1. How do you remove elements from an ArrayList?

                                                               i.      words.remove(1);

                                                             ii.      This decreases the size by 1, and moves all elements down one index

                                                            iii.      Generally –

1.      name.remove(index);

  1. Looping on ArrayLists
    1. Can use either the regular for loop or the enhanced for loop
    2. Example – Names.java
    3. Example – Inventory.java (not finished)
  2. HW – Work on P2, catch up on any reading you’ve missed