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.
