top of page

DSA Java - Arrays

Data Structures and Algorithms (DSA) in Java using an array.

Let's start with a simple example of creating an array, inserting elements, accessing elements, and searching for an element in the array.


As per above shown illustration, following are the important points to be considered.

  • Index starts with 0.

  • Array length is 8 which means it can store 8 elements.

  • Each element can be accessed via its index. For example, we can fetch element at index 6 as 9.



public class ArrayExample {
    private int[] array;
    private int size;

    public ArrayExample(int capacity) {
        array = new int[capacity];
        size = 0;
    }

    public void insert(int element) {
        if (size < array.length) {
            array[size] = element;
            size++;
        } else {
            System.out.println("Array is full. Cannot insert element.");
        }
    }

    public int get(int index) {
        if (index >= 0 && index < size) {
            return array[index];
        } else {
            throw new IndexOutOfBoundsException("Invalid index.");
        }
    }

    public int search(int element) {
        for (int i = 0; i < size; i++) {
            if (array[i] == element) {
                return i;
            }
        }
        return -1;
    }

    public void display() {
        for (int i = 0; i < size; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        ArrayExample arrayExample = new ArrayExample(5);

        arrayExample.insert(10);
        arrayExample.insert(20);
        arrayExample.insert(30);
        arrayExample.insert(40);
        arrayExample.insert(50);

        arrayExample.display(); // Output: 10 20 30 40 50

        System.out.println("Element at index 2: " + arrayExample.get(2)); // Output: 30

        int searchElement = 40;
        int searchResult = arrayExample.search(searchElement);
        if (searchResult != -1) {
            System.out.println("Element " + searchElement + " found at index " + searchResult);
        } else {
            System.out.println("Element " + searchElement + " not found in the array.");
        }
    }
}

In this example, we have a class ArrayExample that represents an array. It has an underlying integer array array, a variable size to keep track of the number of elements in the array, and various methods to perform operations on the array.


The insert method inserts an element into the array if there is space available. The get method retrieves the element at a specified index. The search method searches for a given element in the array and returns its index if found, or -1 if not found. The display method prints the elements of the array.


In the main method, we create an instance of ArrayExample with a capacity of 5. We insert five elements into the array, display the elements, retrieve an element at a specific index, and search for an element in the array.

When you run this code, it will output:



10 20 30 40 50 
Element at index 2: 30
Element 40 found at index 3



Related Posts

See All

DSA Java - Stack

In computer science, a stack is an abstract data type that represents a collection of elements with a last-in, first-out (LIFO) order of access. This means that the most recently added element is the

Java Algorithms

Java is a popular programming language that provides a wide range of tools and libraries for implementing various algorithms. Here are some commonly used algorithms in Java: Sorting Algorithms: Bubble

Java - Data Structures

When it comes to Data Structures and Algorithms (DSA) in Java, there are several commonly used data structures that can be implemented. Here are some of the main data structures and their correspondin

Comentários


bottom of page