Java ArrayLists and Wrapper Classes Study Notes
Understanding ArrayLists
Definition of ArrayList
- An ArrayList is a resizable array implementation in Java. It can hold a dynamically sized collection of elements.
- Elements can be added, removed, or accessed using index positions.
- Unlike traditional arrays, ArrayLists can grow in size as needed.Key Differences Between Arrays and ArrayLists
- Arrays are fixed in size; once created, their length cannot change.
- ArrayLists can grow as you add more elements.
- Words are stored in an ArrayList, whereas the length of a word refers to the number of characters in a string, not related to the ArrayList.
Creating ArrayLists
Creating an ArrayList
- Syntax for creating an ArrayList:java ArrayList<String> wordsFive = new ArrayList<string>();
- This creates an empty ArrayList namedwordsFivethat can holdStringobjects.Adding Elements to the ArrayList
- Adding an element can be done with the following command:java wordsFive.add(word);
- This command adds the specifiedword(a string) into thewordsFiveArrayList.
- The conditionif (k >= 5) { ... }checks if the number of elements has reached or exceeded five before allowing the addition of new words.Returning the ArrayList
- The method completes by returning thewordsFiveArrayList, which now may contain some strings after adding elements.
Wrapper Classes and Primitive Data Types
Primitive Data Types vs. Wrapper Classes
- Java does not allow primitive types directly in an ArrayList. Instead, it uses wrapper classes that allow the representation of these primitives as objects.
- Primitive Data Types:
-int(integer)
-double(double-precision floating point)
-boolean(true/false value)
-char(character)
- Wrapper Classes:
-Integerforint
-Doublefordouble
-Booleanforboolean
-Characterforchar
- The naming convention involves capitalizing the first letter of the wrapper class names.
- Example:Doubleis used asbig D double, anddoubleis referred to aslittle d double(the primitive).Purpose of Wrapper Classes
- Wrapper classes "wrap" the primitive data types in an object shell, allowing them to be stored in collections such as ArrayLists.
- Example of declaring an ArrayList with doubles:java ArrayList<Double> samples = new ArrayList<Double>();
Autoboxing in Java
Definition of Autoboxing
- Autoboxing is the automatic conversion of primitive types into their corresponding wrapper class objects by the Java compiler.
- When adding a primitive value into an ArrayList of wrapper type, the compiler performs autoboxing implicitly.Example of Autoboxing
- Adding a double primitive into an ArrayList of Doubles:java samples.add(5.0);
- In this line,5.0(a double value) is automatically converted to aDoubleobject, allowing it to be stored in the ArrayList.Restrictions on Autoboxing
- Autoboxing does not apply in cases where type conversions are also needed.
- Example that fails:java samples.add(5);
- This line will not work because5is anint, and the Java compiler does not perform both autoboxing and type conversion simultaneously.General Rule
- Learn the rules of autoboxing, as they generally are uni-directional:
- It can convert from a primitive to a wrapper object (autoboxing) but cannot convert from a wrapper back to a primitive and perform type conversion in one step.