Using Arrays and Collections in Java
Introduction​
Arrays and collections are fundamental structures in Java that allow you to store and manipulate groups of data efficiently. While arrays are fixed in size and type, collections provide more flexible and dynamic data handling capabilities.
Using Arrays​
Declaration and Initialization​
Single-Dimensional Arrays​
int[] numbers = {1, 2, 3, 4, 5};
String[] names = {"Alice", "Bob", "Charlie"};
Multi-Dimensional Arrays​
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Accessing Array Elements​
int firstNumber = numbers[0]; // Accessing the first element
int matrixElement = matrix[1][2]; // Accessing the element at row 2, column 3
Modifying Array Elements​
numbers[1] = 10; // Changing the second element to 10
matrix[0][0] = 99; // Changing the first element of the first row to 99
Iterating Over Arrays​
Using For Loop​
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
}
Using Enhanced For Loop​
for (int number : numbers) {
System.out.println(number);
}
Using Collections​
Java's Collections Framework provides a set of classes and interfaces for managing groups of objects. Collections offer more flexibility compared to arrays, such as dynamic sizing and various data structures like lists, sets, and maps.
List Interface​
ArrayList​
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
// Adding elements
names.add("Alice");
names.add("Bob");
names.add("Charlie");
// Accessing elements
System.out.println(names.get(0)); // Outputs: Alice
// Modifying elements
names.set(1, "Robert");
// Removing elements
names.remove(2);
// Iterating over the ArrayList
for (String name : names) {
System.out.println(name);
}
}
}
Set Interface​
HashSet​
import java.util.HashSet;
public class Main {
public static void main(String[] args) {
HashSet<String> names = new HashSet<>();
// Adding elements
names.add("Alice");
names.add("Bob");
names.add("Alice"); // Duplicate element will not be added
// Checking if an element exists
if (names.contains("Alice")) {
System.out.println("Alice is in the set.");
}
// Iterating over the HashSet
for (String name : names) {
System.out.println(name);
}
}
}
Map Interface​
HashMap​
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
HashMap<String, Integer> map = new HashMap<>();
// Adding key-value pairs
map.put("Apple", 1);
map.put("Banana", 2);
map.put("Orange", 3);
// Accessing values
System.out.println("Banana: " + map.get("Banana"));
// Checking if a key exists
if (map.containsKey("Apple")) {
System.out.println("Apple is in the map.");
}
// Iterating over the HashMap
for (String key : map.keySet()) {
System.out.println(key + ": " + map.get(key));
}
}
}
Queue Interface​
LinkedList​
import java.util.LinkedList;
import java.util.Queue;
public class Main {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
// Adding elements
queue.add("Alice");
queue.add("Bob");
queue.add("Charlie");
// Accessing and removing the head of the queue
System.out.println("Head: " + queue.poll());
// Iterating over the Queue
for (String name : queue) {
System.out.println(name);
}
}
}
Conclusion​
Using arrays and collections in Java provides various ways to handle groups of data effectively. Arrays are suitable for fixed-size data, while collections offer dynamic and flexible data structures. Understanding both allows you to choose the appropriate data structure based on your specific needs and application requirements.