What's wrong with arrays?
Terms
- Dynamic data structure
- A structure whose "shape" can change as data changes
- Static data structure
- A structure whose "shape" is fixed upon creation
- Vector
- A java class which represents a dynamic "array"
- ArrayList
- A java class which represents a dynamic "array"
(it's more like a list than an array)
Class Notes
Choices for lab on Tuesday
- Programming contest - A chance to work in teams to complete
the most assignments. This provides practice in problem
solving using a number of different techniques. (Prizes will
be awarded).
- Print f - Everything you ever wanted to know about
formatting output using the String.format() or printf() methods.
This one helps you to format anything in any way that you
wish using a single tool.
- Calvin Snowmen - Learn about Applets and launching them on
a web site.
- Other ideas??
Followup from the Lab
Important items:
- Need to instantiate the arrays before you use them.
- A sequential search (or any search) needs to have a way to
determine if we cannot find the element
- A sequential search (or any search) should have a way to
end if we find the element we are looking for
- Concurrent arrays require us to keep track of the same
element in both arrays.
- Variable length argument lists are handled inside of the
method as arrays...the special ... notation simply allows the values to
be passed in a comma separated list or as an array.
Countries.java
CountryDriver.java
Array List example
An ArrayList (found in the java.util package) is a data structure that
provides a dynamic array capability.
http://java.sun.com/javase/6/docs/api/
Methods include: add, get, clear, clone, remove, isEmpty ...
ArrayListDemo1.java
-
Basic array list with several items added in sequential order
ArrayListDemo2.java-
Demonstrates the toString
ArrayListDemo3.java
- Demonstrates removing an item from an ArrayList
ArrayListDemo4.java
-
Demonstrates inserting an item from the ArrayList
ArrayListDemo5.java
- Demonstrates
the set method
ArrayListDemo6.java-
Casting a generic element with one of a more specific type
ArrayListDemo7.java
- Making
the ArrayList "type safe"
Vector examples
A Vector (found in the java.util package) is a data structure that
provides a dynamic array capability.
http://java.sun.com/javase/6/docs/api/
Methods include: add, get, clear, clone, remove, isEmpty ...
VectorDemo1.java
-
Basic array list with several items added in sequential order
VectorDemo2.java-
Demonstrates the toString
VectorDemo3.java
- Demonstrates removing an item from a Vector
VectorDemo4.java
-
Demonstrates inserting an item from the Vector
VectorDemo5.java
- Demonstrates
the set method
VectorDemo6.java-
Casting a generic element with one of a more specific type
VectorDemo7.java
- Making
the Vector "type safe"