Programming Logic and Design (CpE 112L)
ARRAY OPERATIONS IN JAVA
Introduction:
Arrays are fundamental data structures in Java that allow programmers to store multiple elements of the same
type in a single variable. They provide an efficient way to group data and access it using an index. Arrays are
widely used in programming for tasks like data storage, manipulation, and processing due to their simplicity and
ease of use. This lab will guide you through the basics of arrays, including their declaration, initialization, access,
and common operations. By the end of this lab, you will have a solid understanding of how to work with arrays in
Java.
Objectives:
To create a Java program that allows the user to perform basic operations on an integer array, including
displaying, adding, removing, updating, and searching for elements.
Prerequisites:
Basic knowledge of Java syntax.
Familiarity with arrays and basic input/output in Java.
Task
Each of the following steps will involve implementing a specific array operation. Complete each step and test
your code before moving to the next.
Operation 1: Displaying an Array
This operation will display all elements in the array in a readable format.
Sample Code:
import java.util.Scanner;
public class array {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner (System.in);
int [] grades;
System.out.print("Enter the size of your Array: ");
int size = scan.nextInt();
grades = new int [size];
for (int i = 0; i < size; i++){
System.out.print("Enter an element: ");
int element = scan.nextInt();
grades [i] = element;
}
Southern Leyte State University
Faculty of Engineering
Computer Engineering Department
Programming Logic and Design (CpE 112L)
displayArray(grades, size);
public static void displayArray(int[] grades, int size) {
System.out.print("Array elements: ");
for (int i = 0; i < size; i++) {
System.out.print(grades[i] + " ");
}
}
}
Explanation
The displayArray method iterates through the array and prints each element. This function is useful to
verify the current state of the array after each operation.
Operation 2: Adding an Element
This operation allows the user to add an element to the end of the array, resizing the array if needed.
Sample Code:
import java.util.Scanner;
public class ArrayUpdater {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Get the size of the array from the user
System.out.print("Enter the initial size of your array: ");
int size = scanner.nextInt();
int[] array = new int[size];
// Step 2: Populate the array with elements
for (int i = 0; i < size; i++) {
System.out.print("Enter element " + (i + 1) + ": ");
array[i] = scanner.nextInt();
}
// Step 3: Prompt user to add a new element
System.out.print("Enter an element to add to the array: ");
int elementToAdd = scanner.nextInt();
// Step 4: Add the element to the array, resizing if necessary
array = addElement(array, size, elementToAdd);
// Step 5: Print the updated array
System.out.print("Updated array: ");
Southern Leyte State University
Faculty of Engineering
Computer Engineering Department
Programming Logic and Design (CpE 112L)
for (int num : array) {
System.out.print(num + " ");
}
scanner.close();
}
// Method to add an element, resizing the array if it's full
public static int[] addElement(int[] array, int size, int element) {
if (size < array.length) {
array[size] = element; // Add element if there's space
return array;
} else {
// Resize the array by creating a new array with one extra space
int[] newArray = new int[size + 1];
System.arraycopy(array, 0, newArray, 0, size); // Copy elements
newArray[size] = element; // Add new element
return newArray; // Return the new array
}
}
}
Explanation
addElement creates a new array with one extra slot and copies the original elements to it, adding the
new element at the end.
Operation 3: Remove Element Operation
This operation allows the user to remove a specified element from the array, returning an updated array without
that element.
Sample Code:
import java.util.Scanner;
public class ArrayRemover {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Get the size of the array from the user
System.out.print("Enter the size of your array: ");
int size = scanner.nextInt();
int[] array = new int[size];
// Step 2: Populate the array with elements
for (int i = 0; i < size; i++) {
System.out.print("Enter element " + (i + 1) + ": ");
array[i] = scanner.nextInt();
}
Southern Leyte State University
Faculty of Engineering
Computer Engineering Department
Programming Logic and Design (CpE 112L)
// Step 3: Prompt user to remove an element
System.out.print("Enter an element to remove from the array: ");
int elementToRemove = scanner.nextInt();
// Remove the element from the array
array = removeElement(array, size, elementToRemove);
// Display the updated array after removal
System.out.print("Updated array: ");
displayArray(array);
scanner.close();
}
// Method to remove an element from the array
public static int[] removeElement(int[] array, int size, int element) {
int indexToRemove = -1;
// Find the index of the element to remove
for (int i = 0; i < size; i++) {
if (array[i] == element) {
indexToRemove = i;
break;
}
}
// If element is not found, return the original array
if (indexToRemove == -1) {
System.out.println("Element not found in the array.");
return array;
}
// Create a new array one size smaller
int[] newArray = new int[size - 1];
// Copy elements, skipping the element to be removed
for (int i = 0, j = 0; i < size; i++) {
if (i != indexToRemove) {
newArray[j++] = array[i];
}
}
System.out.println("Element removed.");
return newArray;
}
// Method to display the array
public static void displayArray(int[] array) {
for (int num : array) {
Southern Leyte State University
Faculty of Engineering
Computer Engineering Department
Programming Logic and Design (CpE 112L)
System.out.print(num + " ");
}
System.out.println();
}
}
Explanation
removeElement creates a new array that omits occurrences of the specified element.
Operation 4: Update Element Operation
This operation allows the user to update the value of a specific element in the array.
Sample Code:
import java.util.Scanner;
public class ArrayUpdater {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Get the size of the array from the user
System.out.print("Enter the size of your array: ");
int size = scanner.nextInt();
int[] array = new int[size];
// Step 2: Populate the array with elements
for (int i = 0; i < size; i++) {
System.out.print("Enter element " + (i + 1) + ": ");
array[i] = scanner.nextInt();
}
// Step 3: Prompt user to update an element
System.out.print("Enter the element you want to update: ");
int elementToUpdate = scanner.nextInt();
System.out.print("Enter the new value: ");
int newValue = scanner.nextInt();
// Update the element in the array
updateElement(array, size, elementToUpdate, newValue);
// Display the updated array
System.out.print("Updated array: ");
displayArray(array);
scanner.close();
}
Southern Leyte State University
Faculty of Engineering
Computer Engineering Department
Programming Logic and Design (CpE 112L)
// Method to update an element in the array
public static void updateElement(int[] array, int size, int element, int newValue) {
boolean elementFound = false;
// Search for the element in the array
for (int i = 0; i < size; i++) {
if (array[i] == element) { // If element is found
array[i] = newValue; // Update it with the new value
elementFound = true;
System.out.println("Element updated.");
break;
}
}
if (!elementFound) {
System.out.println("Element not found in the array.");
}
}
// Method to display the array
public static void displayArray(int[] array) {
for (int num : array) {
System.out.print(num + " ");
}
System.out.println();
}
}
Explanation
updateElement searches for the element and updates it with the new value if found.
Operation 5: Update Element Operation
This operation allows the user to search for an element in the array and displays its index if found.
Sample Code:
import java.util.Scanner;
public class ArraySearcher {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Step 1: Get the size of the array from the user
System.out.print("Enter the size of your array: ");
int size = scanner.nextInt();
int[] array = new int[size];
Southern Leyte State University
Faculty of Engineering
Computer Engineering Department
Programming Logic and Design (CpE 112L)
// Step 2: Populate the array with elements
for (int i = 0; i < size; i++) {
System.out.print("Enter element " + (i + 1) + ": ");
array[i] = scanner.nextInt();
}
// Step 3: Prompt user to search for an element
System.out.print("Enter the element you want to search for: ");
int elementToSearch = scanner.nextInt();
// Search for the element in the array
searchElement(array, size, elementToSearch);
scanner.close();
}
// Method to search for an element in the array
public static void searchElement(int[] array, int size, int element) {
boolean elementFound = false;
// Search for the element and print its index if found
for (int i = 0; i < size; i++) {
if (array[i] == element) {
System.out.println("Element found at index: " + i);
elementFound = true;
break;
}
}
if (!elementFound) {
System.out.println("Element not found in the array.");
}
}
}
Explanation
searchElement performs a linear search for the specified element and returns its index if found.
Final Task: Combine All Array Operations into a Single Program
Create a single program that includes all five operations. Add a menu-driven interface to allow the
user to select and perform different operations on an array.
Southern Leyte State University
Faculty of Engineering
Computer Engineering Department