Skip to content

Latest commit

 

History

History
320 lines (255 loc) · 8.47 KB

File metadata and controls

320 lines (255 loc) · 8.47 KB

Understanding ArrayLists in Java

What is an ArrayList?

An ArrayList is a dynamic array that can grow and shrink in size as you add or remove elements. Unlike regular arrays (which have a fixed size), ArrayLists automatically resize themselves when needed.

Think of an ArrayList like a flexible list - you can add items to the end, insert them in the middle, remove them, and the list adjusts its size automatically.

ArrayList vs Array

Regular Array

String[] array = new String[3];  // Fixed size of 3
array[0] = "Apple";
array[1] = "Banana";
array[2] = "Cherry";
// Can't add more items - size is fixed!

ArrayList

ArrayList<String> list = new ArrayList<>();  // Dynamic size
list.add("Apple");
list.add("Banana");
list.add("Cherry");
list.add("Date");     // Can keep adding items!
list.add("Elderberry");

Creating ArrayLists

Basic Creation

import java.util.ArrayList;

// Create empty ArrayList
ArrayList<String> fruits = new ArrayList<>();
ArrayList<Integer> numbers = new ArrayList<>();

// With initial capacity (optional optimization)
ArrayList<String> bigList = new ArrayList<>(100);

Generic Types

ArrayLists use generics to specify what type of objects they hold:

  • ArrayList<String> - holds Strings
  • ArrayList<Integer> - holds Integers
  • ArrayList<Boolean> - holds Booleans

Essential ArrayList Operations

1. Adding Elements

ArrayList<String> list = new ArrayList<>();

// Add to end
list.add("First");      // [First]
list.add("Second");     // [First, Second]

// Add at specific position
list.add(1, "Middle");  // [First, Middle, Second]

// Add at beginning
list.add(0, "Start");   // [Start, First, Middle, Second]

2. Getting Size and Checking Empty

int size = list.size();           // Number of elements
boolean isEmpty = list.isEmpty(); // true if no elements

3. Accessing Elements

// Get element at index (0-based)
String first = list.get(0);       // First element
String last = list.get(list.size() - 1);  // Last element

// Check bounds first to avoid IndexOutOfBoundsException
if (index >= 0 && index < list.size()) {
    String item = list.get(index);
}

4. Searching and Contains

// Check if contains element
boolean hasApple = list.contains("Apple");

// Find position of element
int position = list.indexOf("Apple");  // -1 if not found

// Find last occurrence
int lastPos = list.lastIndexOf("Apple");

5. Removing Elements

// Remove by index
String removed = list.remove(0);        // Remove first element

// Remove by value (removes first occurrence)
boolean wasRemoved = list.remove("Apple");

// Remove all elements
list.clear();

6. Modifying Elements

// Replace element at index
list.set(0, "New Value");  // Changes element at position 0

Common ArrayList Patterns

Adding Multiple Items

ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");

// Or use Collections.addAll
Collections.addAll(fruits, "Date", "Elderberry", "Fig");

Iterating Through ArrayList

// Using traditional for loop
for (int i = 0; i < list.size(); i++) {
    String item = list.get(i);
    System.out.println(item);
}

// Using enhanced for loop (for-each)
for (String item : list) {
    System.out.println(item);
}

Finding Maximum/Minimum

public String findLongest(ArrayList<String> list) {
    if (list.isEmpty()) return null;
    
    String longest = list.get(0);
    for (String item : list) {
        if (item.length() > longest.length()) {
            longest = item;
        }
    }
    return longest;
}

Copying ArrayList

// Shallow copy (recommended)
ArrayList<String> copy = new ArrayList<>(original);

// Alternative method
ArrayList<String> copy2 = (ArrayList<String>) original.clone();

Converting to Array

// To Object array
Object[] objArray = list.toArray();

// To specific type array
String[] stringArray = list.toArray(new String[0]);

ArrayList Methods Summary

Method Description Example
add(item) Add to end list.add("Apple")
add(index, item) Add at position list.add(0, "First")
get(index) Get element list.get(2)
set(index, item) Replace element list.set(1, "New")
remove(index) Remove by index list.remove(0)
remove(item) Remove by value list.remove("Apple")
size() Get size list.size()
isEmpty() Check if empty list.isEmpty()
contains(item) Check if contains list.contains("Apple")
indexOf(item) Find position list.indexOf("Apple")
clear() Remove all list.clear()

Working with Different Types

ArrayList of Integers

ArrayList<Integer> numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);

int sum = 0;
for (int num : numbers) {
    sum += num;
}

ArrayList of Custom Objects (Preview)

// You'll learn this later, but here's a preview:
ArrayList<Person> people = new ArrayList<>();
// person objects would go here

Common Mistakes to Avoid

1. Index Out of Bounds

ArrayList<String> list = new ArrayList<>();
// String item = list.get(0);  // IndexOutOfBoundsException!

// Safe approach:
if (!list.isEmpty()) {
    String item = list.get(0);
}

2. Modifying List While Iterating

// Dangerous - can cause ConcurrentModificationException
for (String item : list) {
    if (item.equals("Remove")) {
        list.remove(item);  // Don't do this!
    }
}

// Safe approach - iterate backwards
for (int i = list.size() - 1; i >= 0; i--) {
    if (list.get(i).equals("Remove")) {
        list.remove(i);
    }
}

3. Forgetting to Handle Null Lists

// Dangerous
public int getSize(ArrayList<String> list) {
    return list.size();  // NullPointerException if list is null!
}

// Safe
public int getSize(ArrayList<String> list) {
    return (list == null) ? 0 : list.size();
}

4. Using Raw Types

// Bad - no type safety
ArrayList list = new ArrayList();  // Raw type
list.add("String");
list.add(123);  // Compiler won't catch this error

// Good - type safe
ArrayList<String> list = new ArrayList<>();  // Generic type
list.add("String");
// list.add(123);  // Compiler error - good!

ArrayList Performance

Time Complexity

  • Add to end: O(1) average, O(n) worst case (when resize needed)
  • Add at position: O(n) - needs to shift elements
  • Get by index: O(1) - direct access
  • Remove by index: O(n) - needs to shift elements
  • Search (contains): O(n) - linear search

When to Use ArrayList

  • Good for: Frequent additions to end, random access by index, iteration
  • Less good for: Frequent insertions/deletions in middle, searching without index

What You Need to Know for the Exercises

  1. Basic Operations: add(), get(), size(), isEmpty()
  2. Safe Access: Check bounds and null before accessing
  3. Iteration: Both traditional and enhanced for loops
  4. Searching: contains(), indexOf(), counting occurrences
  5. Modification: set(), remove(), clear()
  6. Conversion: toArray(), copying lists
  7. Generics: Always specify the type (ArrayList)

Practical Applications

ArrayLists are used everywhere:

  • Shopping Lists: Add/remove items dynamically
  • User Collections: Lists of users, products, orders
  • Data Processing: Collecting results from calculations
  • Menu Systems: Dynamic menu items
  • File Processing: Storing lines read from files
  • Game Development: Lists of players, items, scores

Tips for Success

  1. Always Use Generics: ArrayList<String> not ArrayList
  2. Check Bounds: Verify index is valid before accessing
  3. Handle Null: Check if ArrayList itself is null
  4. Use Enhanced For: Cleaner code when just iterating
  5. Consider Performance: Adding to middle is slow for large lists
  6. Use Meaningful Names: students, orders, items not list1, list2

ArrayList vs Other Collections

  • ArrayList: Dynamic array, good for indexed access
  • LinkedList: Good for frequent insertions/deletions
  • HashSet: No duplicates, fast searching
  • HashMap: Key-value pairs

For now, focus on ArrayList - it's the most commonly used collection in Java!

Remember: ArrayList is like a flexible array that grows and shrinks automatically. Master the basic operations, always handle edge cases, and you'll be able to work with dynamic collections of data effectively!