2 - Array Notes Proper
2 - Array Notes Proper
Arrays in Java
Definition
An array is a collection of elements of the same type stored in a contiguous
memory location. It allows storing multiple values in a single variable, which
makes it easy to access and manipulate the data.
Key Characteristics
1. Fixed Size: The size of the array is defined when it's created and cannot be
changed.
Example:
Array 1
2. Types of Arrays
1. Single-Dimensional Arrays: A list-like structure.
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Modifying Elements:
4. Length of an Array
Array 2
You can get the size of an array using .length :
5. Traversing Arrays
Using For Loop:
Example:
Array 3
1. Allocate a block of memory that can store 5 integers.
Example:
Suppose:
Memory layout:
1 20 1004
2 30 1008
3 40 1012
4 50 1016
To access arr[3] :
Array 4
The value stored at address 1012 is 40 .
Since each element is stored sequentially, you can calculate the exact
memory address of any element using the formula above. This means you
can access any element directly without needing to traverse the array,
resulting in very fast ( O(1) ) access times.
Example: If you need the 5th element, you don't have to start from the
beginning and count each element; you can jump straight to the calculated
address.
When data is stored contiguously, the CPU can load multiple elements into
the cache at once. This is because modern processors read data in
chunks (called cache lines). If elements are placed next to each other,
accessing one will likely load nearby elements, speeding up operations
like loops.
When you know that all elements are stored together, it simplifies the
process of managing memory, as you can treat the whole array as a single
block of memory.
Array 5
// 10
System.out.println("Element at index 3: " + arr[3]);
// 40
Output:
Element at index 0: 10
Element at index 3: 40
Calculated address of arr[3]: 1012
Advantage of Linked List: You can easily grow or shrink the structure without
worrying about memory constraints, as each element is independently
allocated.
Array 6
Disadvantage of Linked List: Accessing elements is slower ( O(n) ) because
you have to traverse through each element from the start to reach the desired
position.
1. Array Copying:
2. Array Sorting:
3. Searching an Element:
4. Filling an Array:
7. Multi-Dimensional Arrays
Declaration:
Array 7
int[][] matrix = new int[3][3]; // 3x3 matrix
Accessing Elements:
Example:
8. Example Programs
1. Sum of Array Elements:
Array 8
System.out.println("Sum of array elements: " + su
m);
}
}
3. Transpose of a Matrix:
Array 9
int[][] transpose = new int[3][3];
// Print Transpose
for (int i = 0; i < transpose.length; i++) {
for (int j = 0; j < transpose[i].length; j++)
{
System.out.print(transpose[i][j] + " ");
}
System.out.println();
}
}
}
Disadvantages:
Inefficient for insertion and deletion (as elements may need to be shifted).
10. Summary
Arrays are fundamental, fixed-size data structures.
Array 10
You can efficiently access and modify elements using their index.
Java provides the Arrays class for operations like sorting, searching, and
copying.
Practice Problems
1. Write a program to reverse an array.
These notes cover the essential concepts and provide practical examples to help
you understand how to work with arrays in Java. Let me know if you'd like to go
deeper into any specific aspect!
In Java, you can implement array operations using the concept of Abstract Data
Types (ADT). An ADT defines a data structure conceptually, including the
operations that can be performed on it, without specifying how those operations
are implemented.
Let's consider an Array ADT and define operations that you might need to
perform, such as insertion, deletion, searching, and traversal. We'll implement
these operations using Java.
Array 11
Implementation of Array ADT in Java
Let's implement an ArrayADT class that encapsulates these operations:
java
Copy code
public class ArrayADT {
private int[] arr;
private int size;
private int capacity;
Array 12
}
// Shift elements to the left
for (int i = index; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--; // Reduce the size
}
Array 13
}
for (int i = 0; i < size; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}
// Inserting elements
array.insert(5);
array.insert(10);
array.insert(15);
array.insert(20);
// Traversing array
System.out.print("Array elements: ");
array.traverse();
Array 14
// Updating an element
array.update(2, 25); // Update the element at index 2
System.out.print("After updating index 2: ");
array.traverse();
// Deleting an element
array.delete(1); // Delete the element at index 1
System.out.print("After deleting index 1: ");
array.traverse();
}
}
Checks if the array has reached its capacity before adding a new element.
2. Delete Operation:
Shifts all elements after the specified index to the left to fill the gap.
3. Search Operation:
Iterates through the array to find the index of the first occurrence of the
given element.
4. Update Operation:
5. Traversal Operation:
Array 15
Useful for checking the current state of the array.
Array elements: 5 10 15 20
Element 15 found at index: 2
After updating index 2: 5 10 25 20
After deleting index 1: 5 25 20
2. Reusability: You can reuse this ArrayADT class in different parts of your
program without rewriting the same logic.
3. Abstraction: Users of the ArrayADT don't need to know how these operations
are implemented; they just need to know how to use them.
Conclusion
The Array ADT example in Java demonstrates how to manage array-based
operations such as insertion, deletion, search, update, and traversal. This way of
encapsulating functionality follows the principles of object-oriented
programming (OOP), where you can easily manage and manipulate arrays
through defined methods.
Feel free to modify or extend this class to add more operations, such as sorting,
reversing, or resizing the array. Let me know if you need more features or
explanations!
Array 16