Lecture on Arrays and ArrayLists
Overview
In this lecture, we covered various aspects of arrays and arraylists, including their definitions, usage, syntax, and memory management in Java. We also explored dynamic memory allocation, array operations, and introduced arraylists as a part of the Java collection framework.
Arrays
What is an Array?
- Collection of data types: can be primitives or objects
- Syntax:
data_type[] arrayName = new data_type[size];
- Example:
int[] rollNumbers = new int[5];
- You can also initialize directly:
int[] rollNumbers = {23, 12, 45, 32, 15};
Key Points
- Data type must be consistent across all elements
- Memory Allocation: Stack and Heap
- Declaration happens at compile time
- Initialization occurs at runtime
Memory Management
- Objects and arrays are stored in heap memory
- Primitives are stored in stack memory
- Dynamic memory allocation: happens at runtime
- Syntax:
new data_type[size]
or by directly initializing the array
Array Indices
- Indexes start from 0
- Access:
arrayName[index]
- Arrays are mutable: elements can be changed
Null Values
- Default values for uninitialized arrays (0 for integers, null for strings, etc.)
null
is a special literal in Java indicating an uninitialized reference
2D Arrays
Definition and Syntax
- Syntax:
data_type[][] arrayName = new data_type[rows][columns];
- Example:
int[][] matrix = new int[3][3];
Internal Representation
- Concept of array of arrays
- Rows must have a defined length, columns can vary
Input and Output Operations
- Use nested loops for input and output
- Enhanced for loop for iteration
ArrayLists
What is an ArrayList?
- Part of the Java collection framework (java.util package)
- Dynamic array: can grow and shrink in size
- Syntax:
ArrayList<data_type> listName = new ArrayList<>();
- Must use wrapper classes for primitives (e.g., Integer for int)
- Internally manages capacity and grows as needed
Operations
- Adding Elements:
list.add(element)
- Accessing Elements:
list.get(index)
- Updating Elements:
list.set(index, element)
- Removing Elements:
list.remove(index)
- Checking for Element:
list.contains(element)
Internal Working
- Initial fixed size, grows dynamically (typically doubles)
- Elements copied to a new array when capacity is exceeded
Multi-dimensional ArrayLists
- ArrayList of ArrayLists:
ArrayList<ArrayList<data_type>> multiList = new ArrayList<>();
- Initialize:
multiList.add(new ArrayList<>());
- Input and output similar to 2D arrays
Practical Examples and Exercises
Swap Elements in an Array
Find Maximum Element in an Array
Reverse an Array using Two Pointers
- Two Pointer Method:
start
and end
pointers, swap until start
exceeds end
Upcoming Topics and Exercises
- Sorting Techniques
- Searching Techniques
- Advanced Array Questions: Rotated array, Subarrays, Sliding window, Two-pointer method
- Array interview questions
Conclusion
- Review and practice examples demonstrated in the lecture
- Separate upcoming session for focused problem-solving and interview questions