Day #1 - Arrays & ArrayLists

Day #1 - Arrays & ArrayLists

Day 1 kicked off with a deep dive into Arrays and ArrayLists in Java. While I've been familiar with Arrays from the start of my coding journey, this time around, I gained fresh perspectives and insights.

Key Insights on Arrays:

First let's get into, how an array is declared:

// creating an array
int[] arr = new int[5];         // size of array = 5

// creating a 2D array
int[][] matrix = new int[3][];
  1. In Java, when you declare and initialize an array, the expression on the left-hand side (LHS) of the equals sign is evaluated during compile time. On the right-hand side (RHS), the statement expression is evaluated during runtime. This process involves dynamic memory allocation, specifically for arrays.

  2. While we commonly understand arrays as data structures that store data in contiguous memory locations, it's important to note that in Java, this isn't always guaranteed. The arrangement in memory is contingent on the Java Virtual Machine (JVM). In Java, array objects find their place in the heap memory, and it's crucial to recognize that, as per Java's specifications, objects in the heap memory need not be contiguous.

  3. If we create a string array like this one:

     String[] str = new String[5]           // size of array = 5
    

    In Java, when working with arrays of strings, the array in the heap memory stores reference variables instead of the actual string values. This is because strings are treated as objects. Consequently, each element in the string array acts as a reference variable, directing to the specific memory location where the actual value is stored.

  4. Why is specifying the number of rows mandatory in a 2D array in Java, while mentioning the number of columns is optional? This requirement stems from the fact that 2D array objects are stored in heap memory, containing references to multiple 1D arrays. Knowing the number of arrays it will hold requires specifying the row count.

    However, the size of each 1D array can vary without limitations. Hence, it's entirely acceptable to omit specifying the column size.

Key Insights on ArrayLists:

// creating an ArrayList
ArrayList<Integer> list = new ArrayList<>();
  1. We use ArrayLists for dynamic array insertion in Java. The assumption is that an ArrayList never hits full capacity. Instead, it dynamically adds elements until reaching a fixed capacity. Once that limit is reached, a new ArrayList is created at a different memory location. The old ArrayList elements are then copied to the new list, allowing the addition of new elements to continue seamlessly.

  2. When working with multidimensional ArrayLists in Java, there's a crucial initial step often overlooked:

     // Multi-dimenional Arraylist
     ArrayList<ArrayList<Integer>> list = new ArrayList<>();
    
     // initializing the Arraylists   --- The IMPORTANT step
     for(int i = 0; i < n; i++) {
         list.add(new ArrayList<>());
     }
    

    This is necessary because initially, there are no sub-ArrayLists within the main 'list' ArrayList. Attempting to add elements without this preliminary step results in an error due to the absence of sub-ArrayLists. This precautionary step becomes essential to ensure a smooth process.

Today's takeaway: explored new concepts on Arrays and ArrayLists. Day 2 gearing up to tackle problems related to the concepts.

Until the next update in the journey... #bytebybyte

~ Future Forward :)