Code Snippets
Runnable Java code examples for testers: arrays, numbers and strings, with output and common mistakes.
Find the Sum of Each Row in a 2D Array in Java (Step-by-Step)
Learn how to calculate the sum of each row in a Java 2D array using nested loops and streams, with full code examples and jagged array support.
Find the Sum of All Elements in a 2D Array in Java
Learn how to calculate the sum of all elements in a Java 2D array using nested loops and streams, including jagged array handling.
Create and Print a 2D Array in Java (Complete Beginner Guide)
Learn how to create, initialize, and print a 2D array in Java using nested loops and Arrays.deepToString(), including jagged arrays.
Convert an ArrayList to an Array in Java (toArray() Fully Explained)
Learn how to convert an ArrayList to an array in Java using toArray(), the zero-length array idiom, and streams, with full examples
Convert an Array to an ArrayList in Java
Learn every way to convert an array to an ArrayList in Java, including the Arrays.asList() trap and primitive array gotchas
Print an Array in Reverse Order Without Reversing It in Java
Learn how to print a Java array in reverse order without modifying the original array, using loops, streams, and ListIterator.
Find the Majority Element in a Java Array (Boyer-Moore Voting)
Learn how to find the majority element (appearing more than n/2 times) in a Java array using the Boyer-Moore Voting Algorithm, O(1) space
Check if an Array Is Sorted or Not in Java (Ascending & Descending)
Learn how to check if a Java array is sorted in ascending or descending order using a single pass loop, with full code and edge cases
Find a Pair with a Given Sum in a Java Array (Two Sum, O(n) Solution)
Learn how to find a pair of elements summing to a target value in a Java array using HashSet and two-pointer techniques, with full code.
Remove a Specific Element from an Array in Java
Learn how to remove a specific element from a Java array by value or index, using the shift technique and ArrayList conversion.
Move All Negative Numbers to the Beginning of a Java Array
Learn to move all negative numbers to the beginning of a Java array using two-pointer partitioning, with order-preserving and non-preserving methods
Move All Zeros to the Beginning of an Array in Java
Learn how to move all zeros to the beginning of a Java array in-place using a reverse two-pointer technique, with full code examples.
Move All Zeros to the End of an Array in Java
Learn how to move all zeros to the end of a Java array in-place using the two-pointer technique, preserving the order of non-zero elements
Find the Index of an Element in a Java Array (Linear & Binary Search)
Learn how to find the index of an element in a Java array using linear search, binary search, and streams, with complexity comparisons.
Rotate an Array to the Right in Java (Reversal Algorithm Guide)
Learn how to rotate a Java array to the right by k positions using the reversal algorithm and extra array method, with full examples
Rotate an Array to the Left in Java
Learn how to rotate a Java array to the left by k positions using extra array, juggling algorithm, and reversal techniques
Swap Two Elements in a Java Array (Temp Variable & XOR Methods)
Learn how to swap two elements in a Java array using a temp variable, arithmetic, and XOR, with clear explanations of why each works.
Replace an Element at a Specific Index in a Java Array
Learn how to replace an element at a specific index in a Java array safely, with validation, examples, and common mistakes to avoid.
Find Element at a Specific Index in a Java Array (Beginner Guide)
Learn how to access an array element at a specific index in Java, handle out-of-bounds errors safely, and understand zero-based indexing
Find Common Elements Between Two Arrays in Java (HashSet Method)
Learn how to find common elements between two arrays in Java using HashSet, retainAll(), and nested loops, with complexity comparisons.