Arrays help us store many values in one place. Common operations let us add, find, or change these values easily.
Common array operations in Java
public class ArrayOperations { private int[] array; private int size; public ArrayOperations(int capacity) { array = new int[capacity]; size = 0; } public void add(int value) { if (size < array.length) { array[size] = value; size++; } } public int find(int value) { for (int i = 0; i < size; i++) { if (array[i] == value) { return i; // return index } } return -1; // not found } public void update(int index, int newValue) { if (index >= 0 && index < size) { array[index] = newValue; } } public void printAll() { for (int i = 0; i < size; i++) { System.out.print(array[i] + " "); } System.out.println(); } public int countOccurrences(int value) { int count = 0; for (int i = 0; i < size; i++) { if (array[i] == value) { count++; } } return count; } }
This class uses a fixed-size array and a size variable to track how many items are added.
Indexing starts at 0, so the first element is at index 0.
ArrayOperations arr = new ArrayOperations(5); arr.add(10); arr.add(20); arr.add(30); arr.printAll();
ArrayOperations arr = new ArrayOperations(3); arr.printAll();
ArrayOperations arr = new ArrayOperations(3); arr.add(5); arr.update(0, 15); arr.printAll();
ArrayOperations arr = new ArrayOperations(3); arr.add(7); arr.add(7); arr.add(2); int count = arr.countOccurrences(7); System.out.println(count);
This program shows how to add numbers to the array, find an element's index, update a value, print all elements, and count occurrences of a value.
public class Main { public static void main(String[] args) { ArrayOperations numbers = new ArrayOperations(5); System.out.println("Before adding elements:"); numbers.printAll(); numbers.add(10); numbers.add(20); numbers.add(30); numbers.add(20); System.out.println("After adding elements:"); numbers.printAll(); int index = numbers.find(20); System.out.println("Index of 20: " + index); numbers.update(1, 25); // change 20 at index 1 to 25 System.out.println("After updating index 1 to 25:"); numbers.printAll(); int count20 = numbers.countOccurrences(20); System.out.println("Count of 20: " + count20); } } class ArrayOperations { private int[] array; private int size; public ArrayOperations(int capacity) { array = new int[capacity]; size = 0; } public void add(int value) { if (size < array.length) { array[size] = value; size++; } } public int find(int value) { for (int i = 0; i < size; i++) { if (array[i] == value) { return i; } } return -1; } public void update(int index, int newValue) { if (index >= 0 && index < size) { array[index] = newValue; } } public void printAll() { for (int i = 0; i < size; i++) { System.out.print(array[i] + " "); } System.out.println(); } public int countOccurrences(int value) { int count = 0; for (int i = 0; i < size; i++) { if (array[i] == value) { count++; } } return count; } }
Adding an element takes O(1) time if space is available.
Finding an element takes O(n) time because it may check all elements.
Updating an element is O(1) because you access by index directly.
Counting occurrences is O(n) because it checks all elements.
Common mistake: Trying to add more elements than the array capacity causes no addition.
Use arrays when you know the maximum size and want fast access by index.
Arrays store multiple values in order and allow easy access by position.
Common operations include adding, finding, updating, printing, and counting values.
Understanding these helps manage lists of data simply and efficiently.
Practice
Solution
Step 1: Understand Java array declaration syntax
In Java, arrays are declared with the type followed by square brackets, then the variable name, and initialized with 'new' and size.Step 2: Check each option for correct syntax
int[] numbers = new int[5]; uses correct syntax: int[] numbers = new int[5]; Options A, B, and D have syntax errors or missing parts.Final Answer:
int[] numbers = new int[5]; -> Option DQuick Check:
Correct array declaration = int[] numbers = new int[5]; [OK]
- Missing square brackets after type
- Not using 'new' keyword
- Incorrect variable initialization
arr in Java?Solution
Step 1: Recall Java array indexing
Java arrays use zero-based indexing, so the first element is at index 0, second at 1, third at 2.Step 2: Identify correct syntax for accessing elements
Elements are accessed with square brackets []. So arr[2] accesses the third element. Options A and D use incorrect syntax, and C accesses the fourth element.Final Answer:
arr[2]; -> Option BQuick Check:
Third element index = 2 [OK]
- Using parentheses instead of brackets
- Using 1-based indexing
- Trying to use methods like get() on arrays
int[] nums = {1, 2, 3, 4, 5};
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
}
System.out.println(sum);Solution
Step 1: Understand the array and loop
The array nums contains 1, 2, 3, 4, 5. The loop runs from i=0 to i=4 (nums.length is 5).Step 2: Calculate the sum of all elements
sum = 0 + 1 + 2 + 3 + 4 + 5 = 15.Final Answer:
15 -> Option AQuick Check:
Sum of 1 to 5 = 15 [OK]
- Off-by-one error in loop
- Starting sum at wrong value
- Using wrong loop condition
data, but it causes a runtime error. What is the error?int[] data = {10, 20, 30};
for (int i = 1; i <= data.length; i++) {
System.out.println(data[i]);
}Solution
Step 1: Analyze loop indices and array length
The array has length 3, valid indices are 0, 1, 2. The loop runs i from 1 to 3 inclusive.Step 2: Identify index causing error
When i = 3, data[3] is accessed, which is out of bounds and causes ArrayIndexOutOfBoundsException.Final Answer:
ArrayIndexOutOfBoundsException because loop goes beyond last index -> Option AQuick Check:
Loop index must be less than length [OK]
- Using <= instead of < in loop condition
- Starting loop at 1 instead of 0
- Ignoring zero-based indexing
arr, which code snippet correctly counts how many elements are greater than 10?Solution
Step 1: Understand the counting condition
We want to count elements strictly greater than 10, so condition is arr[i] > 10.Step 2: Check loop indices and condition correctness
int count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] > 10) { count++; } } System.out.println(count); uses correct zero-based indexing and correct condition. int count = 0; for (int i = 1; i <= arr.length; i++) { if (arr[i] > 10) { count++; } } System.out.println(count); has out-of-bounds error. int count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] >= 10) { count++; } } System.out.println(count); counts elements >= 10 (includes 10). int count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] < 10) { count++; } } System.out.println(count); counts elements less than 10, which is wrong.Final Answer:
int count = 0; for (int i = 0; i < arr.length; i++) { if (arr[i] > 10) { count++; } } System.out.println(count); -> Option CQuick Check:
Count elements with arr[i] > 10 using correct loop [OK]
- Using <= instead of < in loop
- Using wrong comparison operator
- Starting loop at 1 causing index errors
Ready to prove it?
Pass the test and this pattern moves unknown
