Data Structures
Section 3: Data Structures
Lesson 1: Arrays and Collections
1.1 Working with Arrays
Arrays in Java are used to store multiple values of the same type. They have a fixed size and are indexed from 0 to length-1.
Example (Arrays):
public class ArraysExample {
public static void main(String[] args) {
// Declaration and initialization of an array
int[] numbers = {1, 2, 3, 4, 5};
// Accessing elements
System.out.println("Element at index 2: " + numbers[2]);
// Iterating through the array
for (int i = 0; i < numbers.length; i++) {
System.out.println("Element at index " + i + ": " + numbers[i]);
}
}
}
1.2 Introduction to Java Collections (ArrayList, HashMap)
Java Collections provide dynamic data structures that can store and manipulate data. Two commonly used collections are ArrayList and HashMap.
Example (ArrayList):
import java.util.ArrayList;
public class ArrayListExample {
public static void main(String[] args) {
// Creating an ArrayList
ArrayList<String> fruits = new ArrayList<>();
// Adding elements
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Orange");
// Accessing elements
System.out.println("Element at index 1: " + fruits.get(1));
// Iterating through the ArrayList
for (String fruit : fruits) {
System.out.println("Fruit: " + fruit);
}
}
}
Example (HashMap):
import java.util.HashMap;
public class HashMapExample {
public static void main(String[] args) {
// Creating a HashMap
HashMap<String, Integer> ages = new HashMap<>();
// Adding key-value pairs
ages.put("John", 25);
ages.put("Alice", 30);
ages.put("Bob", 22);
// Accessing values
System.out.println("Age of Alice: " + ages.get("Alice"));
// Iterating through the HashMap
for (String name : ages.keySet()) {
System.out.println(name + "'s age: " + ages.get(name));
}
}
}
Lesson 2: Generics
2.1 Understanding Generic Types in Java
Generics allow you to create classes and methods that work with different data types while providing type safety.
Example (Generic Class):
// Generic class
class Box<T> {
private T content;
public Box(T content) {
this.content = content;
}
public T getContent() {
return content;
}
}
public class GenericClassExample {
public static void main(String[] args) {
// Creating a Box with Integer content
Box<Integer> integerBox = new Box<>(42);
// Creating a Box with String content
Box<String> stringBox = new Box<>("Hello, Generics!");
// Accessing content
System.out.println("Integer Box Content: " + integerBox.getContent());
System.out.println("String Box Content: " + stringBox.getContent());
}
}
2.2 Using Generics in Classes and Methods
Generics can be used in both classes and methods to create flexible and reusable code.
Example (Generic Method):
// Generic method
class Printer {
public <T> void printElement(T element) {
System.out.println("Element: " + element);
}
}
public class GenericMethodExample {
public static void main(String[] args) {
Printer printer = new Printer();
// Calling the generic method with different data types
printer.printElement(42);
printer.printElement("Hello, Generics!");
printer.printElement(3.14);
}
}
With these data structures and the power of generics, you can efficiently handle collections of data in a type-safe manner. Practice these examples and explore the versatility of arrays, collections, and generics in Java.