Complete Guide to Arrays and Strings in Java
PART 1: ARRAYS in Java
1. What is an Array?
- A container for storing multiple values of the same data type.
- Example: int[] arr = {10, 20, 30};
2. Declaration & Usage
- Declare, assign values, and access using index.
3. Looping Through Arrays
- Use for loop or enhanced for-each loop.
4. Common Problems
- Sum, max/min, even/odd count.
5. Searching
- Linear Search: O(n)
- Binary Search: O(log n), for sorted arrays.
6. Sorting
- Bubble, Selection, Insertion sort.
- Arrays.sort() for built-in sort.
7. Patterns and Advanced Problems
- Rotate array, remove duplicates, second largest, missing number.
8. 2D Arrays
- Matrix operations: transpose, diagonal sum, spiral order.
PART 2: STRINGS in Java
1. What is a String?
- Immutable sequence of characters. Use StringBuilder for modifications.
2. Common String Methods
- length(), toLowerCase(), charAt(), contains()
3. Useful Operations
- Reverse, palindrome check, vowel/consonant count, toggle case.
4. Advanced Modifications
- Compress string (aabbb -> a2b3), remove duplicates, remove digits.
5. Search and Frequency
- Anagram check, string rotation, frequency count, first non-repeating character.
6. Special Tasks
- Replace space with %20, CamelCase splitting, Pangram check.
Tips:
- Practice with dry runs.
- Start simple and increase difficulty.
- Use data structures like HashMap, Set.
- Practice daily on coding platforms.