Lecture 22, CS 302-6, October 24
i. 10pm today-pairs must be registered
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){}
i. You will not be held responsible for recursion on tests etc
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
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
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
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
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
i. Number of elements in the ArrayList
ii. words.size();
iii. Generally –
1. name.size();
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())
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
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
i. words.remove(1);
ii. This decreases the size by 1, and moves all elements down one index
iii. Generally –
1. name.remove(index);