An ArrayList
is a Generic data Type
"Any type": Anything that is not a primitive type
"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
"Any type": Anything that is not a primitive type
Examples:
.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). "Any type": Anything that is not a primitive type
not a primitive type: Objects, fixed-size arrays, Integer, Character, Double, Long, String
Examples:
.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). null
reference:
The previous examples throw standard Java Exceptions. ( Read the red messages printed by the computer )
IndexOutOfBoundsException
NullPointerException
NumberFormatException
try
and catch
statements Your program will first try
to execute the code, and catch
an Exception if an error happens.
A user of this method may pass you an array with null
references. Accessing a null
element will throw a NullPointerException
You can defer error handling by making your code throw an Exception
You can defer error handling by making your code throw an Exception
FileReader
and BufferedReader
classesFileReader
handles the opening and reading of text files, character by character BufferedReader
allows you to read the file line by line instead. FileWriter
and BufferedWriter
classesFileWriter
handles the opening and writing of text files, character by character BufferedWriter
allows you to write multiple characters at once. FileWriter
and BufferedWriter
classesFileWriter
handles the opening and writing of text files, character by character BufferedWriter
allows you to write multiple characters at once. /