Basic Array Operations in Java

By Raunak Goswami Last updated : February 03, 2024

Just like other programming languages, you can also perform various operations on an array in Java. Some of the basic operations that can be performed with an array are:

  1. Creating an array.
  2. Entering values into the array.
  3. Finding the maximum value from the array.
  4. Finding the minimum value from the array.
  5. Searching a particular value in the array using linear search.
  6. Sorting the array using bubble sorting.

Implementation of all basic array operations

In this program, we are implementing all of the basic array operations mentions above.

import java.util.*;

public class Main {
  public static void main(String[] args) {
    int c = 0, k;

    // Creating an object of scanner class 
    // for taking input from the user
    Scanner Sc = new Scanner(;

    System.out.println("Enter the number of elements you want in your array");

    // taking the length of the array from the 
    // user using nextInt() method
    int len = Sc.nextInt();

    // creating an array from the user defined length
    int arr[] = new int[len];
    System.out.println("Enter the elements in the array");

    // taking input from the user using for loop
    for (int i = 0; i < len; i++) {
      // nextInt() method allows the user to 
      // enter the values inside the array
      arr[i] = Sc.nextInt();

    //  temp variable is used to perform sorting 
    // of the variables in the bubble sorting
    int temp = 0;

    // using nested loops for bubble sort  elements
    // in ascending order
    for (int i = 0; i < len; i++) {
      for (int j = i + 1; j < len; j++) {
        if (arr[i] > arr[j]) {
          temp = arr[j];
          arr[j] = arr[i];
          arr[i] = temp;

    System.out.println("The largest element in the array is ");
    // since the elements are sorted in ascending order 
    // the last element is the largest element
    System.out.println(arr[len - 1]);

    System.out.println("The smallest element in the array is");

    // this will print the smallest element

    System.out.println("Enter the element you want to search");
    // taking the input from the user to perform linear search
    int g = Sc.nextInt();

    // for loop to check whether the given element 
    // is present in the array or not
    for (int i = 0; i < len; i++) {
      if (g == arr[i]) {
        // to show the actual indexing since the 
        // indexing of array starts from 0 
        k = i + 1;
        System.out.println("Element found at " + k + "th " + "position");
        c = 1;

    if (c != 1) //  if the element is not found 
      System.out.println("Element not found");

    System.out.println("elements in the sorted order");

    //  for loop to print the list of elements 
    // that we have sorted using bubble sort
    for (int i = 0; i < len; i++) {
      // print statement to print the elements of the array


The output of the above program is:

Enter the number of elements you want in your array

Enter the elements in the array

The largest element in the array is 
The smallest element in the array is

Enter the element you want to search

Element found at 1th position

elements in the sorted order

Try experimenting with the code written above and make your own array related programs in Java, hope you like this article. Have a great day ahead and keep learning.

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.