0% found this document useful (0 votes)
15 views

Arrays

An array is a collection of similar data items that are stored in a linear or grid format: Definition An array is an ordered set of data items that are stored in consecutive memory locations in RAM. Elements All elements in an array have the same data type. Access Elements in an array can be accessed individually by specifying their index value.

Uploaded by

Jeeva Sadhasivam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Arrays

An array is a collection of similar data items that are stored in a linear or grid format: Definition An array is an ordered set of data items that are stored in consecutive memory locations in RAM. Elements All elements in an array have the same data type. Access Elements in an array can be accessed individually by specifying their index value.

Uploaded by

Jeeva Sadhasivam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

I MSC COMPUTER SCIENCE

SUBJECT CODE: 24CSCP101


DATA STRUCTURES AND ALGORITHMS LAB USING JAVA

1. Implement the basic operations on arrays in Java such as insertion,


deletion and traversal.

import java.util.Scanner;
public class ArrayOperations {
private int[] arr;
private int size;
// Constructor to initialize the array and its size
public ArrayOperations(int capacity) {
arr = new int[capacity];
size = 0;
}
// Method to insert an element into the array
public void insert(int element) {
if (size < arr.length) {
arr[size] = element;
size++;
System.out.println("Element inserted successfully.");
} else {
System.out.println("Array is full. Cannot insert element.");
}
}

// Method to delete an element from the array


public void delete(int index) {
if (index >= 0 && index < size) {
for (int i = index; i < size - 1; i++) {
arr[i] = arr[i + 1];
}
size--;
System.out.println("Element deleted successfully.");
} else {
System.out.println("Invalid index. Cannot delete element.");
}
}
// Method to traverse and display the elements of the array
public void traverse() {
System.out.print("Array elements: ");
for (int i = 0; i < size; i++) {
System.out.print(arr[i] + " ");
}
System.out.println();
}

// Main method
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the capacity of the array: ");
int capacity = scanner.nextInt();

ArrayOperations arrayOps = new ArrayOperations(capacity);

while (true) {
System.out.println("\nChoose an operation:");
System.out.println("1. Insert");
System.out.println("2. Delete");
System.out.println("3. Traverse");
System.out.println("4. Exit");

int choice = scanner.nextInt();

switch (choice) {
case 1:
System.out.print("Enter element to insert: ");
int element = scanner.nextInt();
arrayOps.insert(element);
break;
case 2:
System.out.print("Enter index to delete: ");
int index = scanner.nextInt();
arrayOps.delete(index);
break;
case 3:
arrayOps.traverse();
break;
case 4:
System.out.println("Exiting...");
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}

You might also like