ArrayList, Hashtables, Exceptions, and Files



Course Evaluations in Miverva

ArrayList

java.util.ArrayList

This is one of the Java implementations of a LinkedList. I.e. an ArrayList does not have a fixed size.

An ArrayList is a Generic data Type

"Any type": Anything that is not a primitive type

Using java.util.ArrayList

"Any type": Anything that is not a primitive type


Java provides non-primitive type versions of primitive types, that can be used with an ArrayList

  • int → Integer
  • long → Long
  • double → Double
  • char → Character

You can assign a null value to the previous non-primitive types.

You can assign a primitive value/variable to its non-primitive counterpart, only if it is not null

Using java.util.ArrayList

"Any type": Anything that is not a primitive type

Examples:

Using an ArrayList

ArrayList methods: size, add, get, and remove

Using an ArrayList

Other ArrayList methods ( listed in the Java Documentation ):

  • .add( int index, T newElement) → inserts newElement at position given by index
  • .remove( T target ) → removes the first occurrence of the target
  • .indexOf( T target ) → Searches for the target and returns the index of the first occurrence.
  • .lastIndexOf( T target ) → Returns the index of the last occurrence of the target.
  • .clear() → Remove all elements form an array.
  • .toString() → A string representation of the contents (Useful for debugging).

Hashtable

Hashtable

Think of a Hashtable as a dictionary

We use a Hastable to associate a key to a value

Hashtable

Keys, as well as values, can be of "any type"

Using java.util.Hashtable

"Any type": Anything that is not a primitive type


not a primitive type: Objects, fixed-size arrays, Integer, Character, Double, Long, String

Examples:

Using a Hashtable

Hashtable methods: size, put, get and remove

Using a Hashtable

Other Hashtable methods ( listed in the Java Documentation ):

  • .containsKey( K target ) → checks if the Hashtable contains an entry with the target key
  • .containsValue( V target ) → checks if the Hashtable contains an entry with the target value
  • .clear() → Remove all entries from the Hashtable.
  • .toString() → A string representation of the contents (Useful for debugging).

Exceptions

Programmatically handling run-time errors

Exceptions: An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions


Trying to get data from an array, or ArrayList, at an index that is greater that its number of elements

Trying to call a method of a null reference:

Trying to get an integer number, from a string that does not represent an integer

Programmatically handling run-time errors

Exceptions: An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions

The previous examples throw standard Java Exceptions. ( Read the red messages printed by the computer )


IndexOutOfBoundsException

NullPointerException

NumberFormatException

Programmatically handling run-time errors

You can handle the exception by using the try and catch statements

Your program will first try to execute the code, and catch an Exception if an error happens.

Programmatically handling run-time errors

This is useful for when you cannot predict if the data that your code receives is correct; i.e. when you deal with user input.

A user of this method may pass you an array with null references. Accessing a null element will throw a NullPointerException

Defering error handling to the user of the method

You can decide not to handle the error yourself, and let the user of your method do it

You can defer error handling by making your code throw an Exception

Defering error handling to the user of the method

You can also throw an Exception with your own custom message

You can defer error handling by making your code throw an Exception

File Input/Output

Reading a text file

You need the FileReader and BufferedReader classes

Writing a text file

You need the FileWriter and BufferedWriter classes

Writing a text file

You need the FileWriter and BufferedWriter classes

Resources

/