Modify the Array Element in Leftwards in Java

Write a single dimension array in a java program to modify the element located in the leftwards direction. A one-dimensional array in the form of a linear array. To access its items, a single subscript is utilized, which can represent a row or column index. The first element in the temp variable is stored in the last position, a[n-1], and the following elements are shifted one position to the left by saving the element in the current position in the previous position.

import java.util.*;
class Main
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int i,n,temp;
System.out.println("Enter the no of elements") ;
n = sc.nextInt();
int[] a = new int[n];
System.out.println("Enter the elements") ;
for(i=0;i<n;i++)
{
a[i] = sc.nextInt();
}
System.out.println("Before sorting the elements");
for(i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}
temp=a[0];
for(i=0;i<n-1;i++)
{
a[i]=a[i+1];
}
a[n-1]=temp;
System.out.println("\nElements after sorting");
for(i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}
}
}

 

Output:

Enter the no of elements
3
Enter the elements
45
67
89
Before sorting the elements
45 67 89
Elements after sorting
67 89 45

What is array in java?

An array is a data structure that contains a collection of elements. The data types of these items are frequently the same, such as an integer or a string. Arrays are commonly used in computer programs to organize data so that a related group of numbers can be quickly sorted or searched. Arrays can be classified into three types: indexed arrays, multidimensional arrays, and associative arrays. 

Advantages of array in single dimensional program

Using the index number to access an element in an array is very simple.
The search process is simple to apply to an array.
Matrixes are represented using a 2D array.
If a user needs to store multiple values of the same type for any reason, the Array can be used effectively.

Limitations of array programs in java

The dimension of an array is fixed at the time of its creation and cannot be changed later;
Regardless of the number of elements that are actually of interest, the array takes up a proportional amount of memory to its size.

One dimensional array vs multidimensional array in java

A one-dimensional array is a one-dimensional linear array. To access its items, a single subscript is used, which can indicate a row or column index. Consider the C declaration int anArrayName[10], which specifies a one-dimensional array with ten instances. In this case, the array can hold ten int entries. This array’s indices range from zero to nine. The phrases anArrayName[0] and anArrayName[9] are used to represent the first and last elements, respectively.

Sorting algorithm in java using array

Sorting is the process of arranging data in a data structure in a preferred order. Sorting data makes it easier to search through it quickly and efficiently. The most basic example of sorting is a dictionary. A variety of sorting methods are used in computer science.