Arrays and Array Lists ✏ AP Computer Science A

Rucete ✏ AP Computer Science A In a Nutshell

6. Arrays and Array Lists

This chapter covers the fundamentals of one-dimensional arrays, array lists, two-dimensional arrays, array operations, traversals, shuffling algorithms, random element selection, and simultaneous array-array list operations in Java.


One-Dimensional Arrays

• Arrays store elements of the same type with fixed size.

• Indices range from 0 to N-1 for an array of N elements.

• Access elements with arr[index]; accessing invalid indices throws ArrayIndexOutOfBoundsException.

Array Initialization

• Arrays are created using new or initializer lists (e.g., int[] coins = {1, 5, 10, 25};).

• Default initialization: 0 for numbers, false for booleans, null for objects.

Array Length

• Access the number of elements using arr.length (no parentheses).

• Contrast with String.length() which requires parentheses.

Traversing Arrays

• Use enhanced for loops when accessing elements without modifying or needing indices.

• Use standard for loops when modifying elements or needing indices.

Arrays as Parameters

• Passing an array to a method passes the reference, not a copy.

• Changes to array elements inside methods affect the original array.

• Individual primitive elements are passed by value (copied).

Shuffling Arrays

• Use random number generation to shuffle arrays by swapping elements randomly.

• Key: (int)(Math.random() * numberOfElements).

Arrays in Classes

• Arrays can be private instance variables inside classes (e.g., Deck of cards).

• Always initialize each array element separately if it refers to objects.

Analyzing Array Algorithms

• Sequential traversal has best and worst-case scenarios based on the data.

• Inserting or deleting in an ordered array can be inefficient due to element shifting.

ArrayLists

• ArrayList is a resizable array structure provided in java.util package.

• Advantages over arrays:

• Dynamic resizing (can grow or shrink as needed).

• Built-in methods like add(), remove(), get(), set(), size().

• ArrayLists store references to objects (not primitives directly; use wrapper classes like Integer).

ArrayList Initialization and Usage

• ArrayList<Type> list = new ArrayList<>();

• Example: ArrayList<String> words = new ArrayList<>();

• Use methods:

• add(element): Appends to end.

• add(index, element): Inserts at index.

• set(index, element): Replaces element at index.

• remove(index): Removes element at index.

• get(index): Retrieves element at index.

• size(): Returns current number of elements.

Traversing ArrayLists

• Enhanced for loop: Useful when only accessing elements without changing them.

• Standard for loop: Needed when modifying elements or using indices.

Removing Elements from ArrayLists

• Important: When removing elements inside a loop, account for shifting indices.

• Strategy: Traverse backwards when removing elements to avoid skipping.

Two-Dimensional Arrays

• A two-dimensional array is an array of arrays.

• Syntax: int[][] grid = new int[rows][cols];

• Access: grid[row][col].

• Default initialization: 0 for numbers, false for booleans, null for objects.

Traversing 2D Arrays

• Nested loops are used:

• Outer loop: Row traversal.

• Inner loop: Column traversal.

Row-Major and Column-Major Traversal

• Row-major: Complete a row before moving to the next (standard approach).

• Column-major: Complete a column before moving to the next column (less common, but important for some applications).

Key 2D Array Methods

• Length of the array (number of rows): arr.length.

• Length of a particular row (number of columns in that row): arr[row].length.

Common Mistakes with 2D Arrays

• Forgetting that different rows can have different lengths (jagged arrays).

• Confusing row and column indices when accessing elements.

In a Nutshell

Arrays and ArrayLists provide fundamental structures for managing collections of data in Java. Arrays offer fixed-size storage with fast access, while ArrayLists offer flexible, resizable storage with powerful built-in methods. Understanding traversal techniques, element manipulation, 2D array organization, and common pitfalls is crucial for writing efficient and correct data-processing programs.

Post a Comment

Previous Post Next Post