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>(); &nbsp;&nbsp;&nbsp;&nbsp;
      - This creates an empty ArrayList named wordsFive that can hold String objects.

  • Adding Elements to the ArrayList
      - Adding an element can be done with the following command:
    java wordsFive.add(word); &nbsp;&nbsp;&nbsp;&nbsp;
      - This command adds the specified word (a string) into the wordsFive ArrayList.
      - The condition if (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 the wordsFive ArrayList, 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:
        - Integer for int
        - Double for double
        - Boolean for boolean
        - Character for char
      - The naming convention involves capitalizing the first letter of the wrapper class names.
        - Example: Double is used as big D double, and double is referred to as little 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>(); &nbsp;&nbsp;&nbsp;&nbsp;

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); &nbsp;&nbsp;&nbsp;&nbsp;
      - In this line, 5.0 (a double value) is automatically converted to a Double object, 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); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
          - This line will not work because 5 is an int, 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.