Introduction to One-
Dimensional Arrays in Java
A one-dimensional array, often simply called an array, is a fundamental data structure in computer science.
• What it is:
• A collection of elements: An array stores a group of items
(elements) of the same data type (e.g., integers, characters,
strings).
• Ordered: The elements are arranged in a specific sequence,
with each element having a unique position.
• Contiguous memory: The elements are stored in adjacent
memory locations. This allows for efficient access.
• Fixed : In many programming languages, the size of an array
is determined when it's created and cannot be changed later
without creating a new array. Some languages like python
using lists can dynamically change sizes.
How it works:
• Indexing: Each element in an array is accessed using an index, which is a
numerical value representing its position. Typically, indexing starts from
0. So, the first element has an index of 0, the second element has an index
of 1, and so on.
• Accessing elements: To retrieve or modify an element, you use the array's
name and the element's index within square brackets. For example,
array[0] refers to the first element of the array named "array".
• Memory allocation: When an array is created, the computer allocates a
contiguous block of memory large enough to hold all the elements.
• Example:
• Imagine an array called "numbers" that stores the following integers: 10,
20, 30.
• * numbers[0] would be 10.
• * numbers[1] would be 20.
• * numbers[2] would be 30.
Declaration and Initialization
• // Declaration and initialization in separate lines:
int[] myArray; // Declaration: myArray is a reference to an int array
myArray = new int[5]; // Initialization: creates an int array of
size 5
// Initialization with default values (0 for int):
// myArray[0] = 0, myArray[1] = 0, myArray[2] = 0, myArray[3] = 0,
myArray[4] = 0
// Declaration and initialization in a single line:
int[] anotherArray = new int[10]; // Creates an int array of size 10
// Declaration and initialization with specific values (array literal):
int[] numbers = {1, 2, 3, 4, 5}; // Creates an int array with the given
values
• Array of strings: String[] names = {"Alice", "Bob", "Charlie"};
• Array of doubles: double[] prices = {19.99, 24.50, 10.00};
• Array of booleans: boolean[] flags = {true, false, true};
• Array of characters: char[] letters = {'a', 'b', 'c'};
• Accessing array elements: int firstElement = numbers[0]; //
firstElement will be 1 numbers[2] = 10; // Modifies the third element
of the 'numbers' array
• Array length: int length = numbers.length; // length will be 5 in
this case
• Iterating through an array:
for (int i = 0; i < numbers.length; i++)
{
System.out.println(numbers[i]);
}
Characteristics and
• Advantages:
Key characteristics and advantages:
• * Fast access: Because elements are stored contiguously, accessing any
element by its index is very fast (constant time, or O(1)).
• * Simple structure: Arrays are relatively simple to understand and
implement.
• * Efficient storage: Contiguous memory allocation minimizes memory
overhead.
Disadvantages:
• * Fixed size (in some languages): In languages like C and Java, the fixed
size can be a limitation if you need to store a variable number of
elements.
• * Insertion and deletion: Inserting or deleting elements in the middle of
an array can be inefficient, as you may need to shift other elements to
make room or fill the gap.
• * Same data type: all elements within a standard array must be of the
same datatype.