Single-Dimensional Arrays in Java (Part 1)
Introduction to Single-Dimensional Arrays in Java
Overview of the Lecture
- The lecture introduces single-dimensional arrays in Java, outlining key topics such as null values, array creation, default values, accessing elements, array initializers, and printing arrays.
Understanding Null Values
- A null value indicates that an object references nothing. For example, a string variable named
textis initially set to null until assigned a value.
- When a variable is created without assignment, it defaults to null. Assigning a string to the variable changes its state from null to referencing the actual string.
What are Arrays?
Definition and Characteristics
- An array is defined as a collection of variables of the same data type. In Java, arrays are objects that reference groups of data.
- The size of an array is fixed upon creation; for instance, an integer array can hold six integers but cannot exceed this limit.
Memory Allocation
- Elements in an array are stored sequentially in memory. Each element follows the previous one directly.
Creating Arrays
Declaration and Instantiation
- To declare an array in Java, specify the desired data type followed by brackets and assign it a name. Initially, this variable will be null.
- Use the
newkeyword to create an array in memory. For example:arrayName = new DataTypecreates an array with 10 elements.
Example Creation
- An example provided shows how to create an integer array called
numbers, which can hold 20 integers. Once created, its size remains constant at 20 elements.
Default Values in Arrays
Initialization Defaults
- Upon creation:
- Numeric types (int, float, double): initialized to 0.
- Character arrays: initialized to the null character.
- Boolean arrays: initialized to false.
- Object arrays (e.g., strings): initialized to null.
Accessing Array Elements
Access Methodology
- To access elements within an array, use brackets with indices. For instance:
- Accessing the first element requires using index
0.
Modifying Array Values
Understanding Array Manipulation in Programming
Accessing and Modifying Array Elements
- The index of the third element in an array is 2; it can be accessed and modified by assigning a new value, such as setting it to 8.
- To access the last element of an array, use the formula: length of the array minus one. For example, if the last index is 4, you can assign it a new value like 10.
Array Initialization
- Arrays can be initialized with specific values using braces. For instance, creating an array called
numberswith values inside braces will define its size based on the number of elements provided.
- The size of an initialized array is fixed and determined by how many values are placed between the braces during initialization.
Printing Array Values
- When printing an array directly (e.g.,
print(numbers)), it outputs the memory address rather than its contents. To display actual elements, a loop must be used.
- A for loop can iterate through each element in an array starting from index zero up to
numbers.length - 1, allowing access to all elements sequentially.
Iterating Through Arrays
- In a for loop, iteration continues while the index variable (i.e.,
i) is less than the length of the array. This ensures that all elements are printed without exceeding bounds.
- Each iteration prints out individual elements using their respective indices (e.g.,
numbers[i]), providing clear output for each value stored in the array.
Output Results
- The program's output includes both the memory address of the array and its contents printed sequentially based on previous iterations through a loop.