DSA in Java: Arrays & the Two-Pointer Pattern (Beginner Guide)
Master arrays in Java and the powerful two-pointer technique. Learn array time complexity, looping, pair-sum, and in-place reversal with clear beginner examples.
Arrays are the very first data structure you'll reach for in almost every problem — and they're the foundation the rest of DSA builds on. In this lesson we'll get properly comfortable with arrays in Java, and then learn the single most useful array technique for interviews: the two-pointer pattern. Master this and a whole category of problems that looks scary suddenly becomes routine.
What an array really is
An array is a fixed-size, ordered collection of items stored next to each other in memory. That "next to each other" part is why arrays are so fast: because the computer knows exactly where the array starts and how big each item is, it can jump straight to any index in a single step.
int[] nums = {10, 20, 30, 40, 50}; nums[0]; // 10 → jump straight to index 0, O(1) nums[3]; // 40 → jump straight to index 3, O(1) nums.length; // 5
This is the array's superpower: access by index is O(1) — constant time, no matter how big the array is (remember Big O from the last lesson?).
What arrays are good and bad at
| Operation | Time | Why |
|---|---|---|
| Access by index | O(1) |
Direct jump to a memory slot |
| Update by index | O(1) |
Same reason |
| Search (unsorted) | O(n) |
Might check every item |
| Insert / delete in middle | O(n) |
Must shift everything after it |
The takeaway: arrays are brilliant when you know the index, and slower when you need to search or insert in the middle. That trade-off is exactly why other data structures (coming in later lessons) exist.
Looping over an array
int[] nums = {10, 20, 30, 40, 50}; // classic for-loop when you need the index for (int i = 0; i < nums.length; i++) { System.out.println(nums[i]); } // enhanced for-loop when you just need the values for (int n : nums) { System.out.println(n); }
The two-pointer pattern (the star of this lesson)
Here's the idea that unlocks dozens of interview problems. Instead of using nested loops (which are O(n²) — slow!), you use two index variables ("pointers") that move through the array intelligently, usually solving the problem in a single O(n) pass.
The most common setup: one pointer starts at the left end, one at the right end, and they move toward each other.
Worked example: is the sum of any pair equal to a target?
Given a sorted array, find whether two numbers add up to a target. The brute-force way checks every pair (O(n²)). The two-pointer way does it in O(n):
boolean hasPairWithSum(int[] arr, int target) { int left = 0; int right = arr.length - 1; while (left < right) { int sum = arr[left] + arr[right]; if (sum == target) return true; // found it! if (sum < target) left++; // need bigger → move left up else right--; // need smaller → move right down } return false; }
Read that logic slowly — it's the whole trick. Because the array is sorted, if the current sum is too small we know we must move the left pointer right (to a bigger number); if it's too big, we move the right pointer left. Each step throws away a whole set of impossible pairs, so we finish in one linear pass instead of checking every combination.
Reversing an array — the same pattern
Two pointers from both ends also reverse an array in place, using O(1) extra memory:
void reverse(int[] arr) { int left = 0, right = arr.length - 1; while (left < right) { int temp = arr[left]; // swap the two ends arr[left] = arr[right]; arr[right] = temp; left++; right--; } }
A quick note on ArrayList
Java arrays are fixed-size. When you need a resizable array, you use ArrayList from the Collections framework (covered in the Core Java course). Under the hood it's still backed by an array — it just grows automatically for you. For interview algorithm problems, though, plain arrays and two pointers are your bread and butter.
When I first saw the two-pointer trick, it genuinely changed how I approached array problems. I used to reach for nested loops out of habit and end up with slow O(n²) solutions. Now, whenever I see a sorted array — or a problem about pairs, palindromes, or moving from both ends — a little alarm goes off: "can two pointers do this in one pass?" More often than not, they can. Practise this pattern until it's instinct; it shows up constantly, and interviewers love watching you reach for it instead of brute force.
Key takeaways
- An array stores items contiguously, giving O(1) access/update by index, but O(n) search and middle insert/delete.
- The two-pointer pattern uses two indices to solve many problems in a single
O(n)pass instead ofO(n²). - Classic uses: finding a pair with a target sum (sorted array), reversing in place, and palindrome checks.
- Use
ArrayListwhen you need a resizable array; use plain arrays + pointers for algorithm problems.
← Previous: DSA Lesson 1 — Time & Space Complexity
Next: DSA Lesson 3 — Strings & Common Patterns →
↑ Back to the DSA roadmap