Java Program to Decrement Each Element of Array by 1 and Print the Decremented Array

In the previous article, we have seen Java Program to Increment Each Element of Array by 1 and Print the Incremented Array

In this article we are going to see how decrement array element by one using Java programming language.

Java Program to Decrement Each Element of Array by 1 and Print the Decremented Array

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to decrement each element of array by 1 and print the incremented array.

Method-1: Java Program to Decrement Each Element of Array by 1 and Print the Decremented Array By Static Initialization of Array Elements

Approach:

  • Declare and initialize an array.
  • Iterate over the array.
  • Decrement each element by one.
  • Print the array.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        int arr[]={10,20,30,40,50};
        System.out.println("Original array: " + Arrays.toString(arr));
        DecrementByOne(arr);
    }

    private static void DecrementByOne(int[] arr) 
    {
        for (int i = 0; i < arr.length; i++) 
        {
            arr[i]--;
        }
        System.out.println("Decremented array: " + Arrays.toString(arr));
    }

}
Output:

Original array: [10, 20, 30, 40, 50]
Decremented array: [9,19,29,39,49]

Method-2: Java Program to Decrement Each Element of Array by 1 and Print the Decremented Array By Dynamic Initialization of Array Elements

Approach:

  • Take the array size from user.
  • Take input of array elements as user input.
  • Iterate over the array.
  • Decrement each element by one.
  • Print the array.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) 
        {
            arr[i] = sc.nextInt();
        }
        System.out.println("Original array: " + Arrays.toString(arr));
        DecrementByOne(arr);
    }

    private static void DecrementByOne(int[] arr) 
    {
        for (int i = 0; i < arr.length; i++) 
        {
            arr[i]--;
        }
        System.out.println("Decremented array: " + Arrays.toString(arr));
    }

}
Output:

Enter the size of array: 5
Enter array elements: 10 20 30 40 50
Original array: [10, 20, 30, 40, 50]
Decremented array: [9,19,29,39,49]

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Related Java Programs: