Java program to change the element position in right direction using single dimension array

Java program to change the element position in the right direction using a single dimension array. A linear array is a form of a one-dimensional array. A single subscript, which can indicate a row or column index, is used to access its items. The last element is stored in the temp variable and then placed in the starting position, which is a[0], while the other items are shifted from one position to the right by storing the element in the current position to the next 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 number of elements in the array") ;
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");
for(i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}
temp=a[n-1];
for(i=n-1;i>0;i--)
{
a[i]=a[i-1];
}
a[0]=temp;
System.out.println("\nElements after repositioned");
for(i=0;i<n;i++)
{
System.out.print(a[i]+" ");
}
}
}

Output:

Enter the number of elements in the array
3
Enter the elements
34
56
76
Before sorting
34 56 76
Elements after repositioned
76 34 56

What is Array in java programming language and it uses?

A data structure that contains a group of elements is known as an array. These items are often all of the same data type, such as an integer or a string. In a computer program, arrays are widely used to organize data so that a related group of numbers can be readily sorted or searched. 

Array and its uses

Arrays use a single name to represent numerous data elements of the same type. The elements in arrays can be accessed at random using the index number.For all of its elements, arrays allocate memory in contiguous memory regions. As a result, in the case of arrays, there is no probability of extra RAM being allocated. This prevents memory overflow and shortages in arrays. Other data structures, such as linked lists, stacks, queues, trees, and graphs, can be built using arrays. Matrixes are represented using two-dimensional arrays.

Single dimensional Array vs Multi dimensional Array

A one-dimensional array is a linear array of one dimension. A single subscript, which can indicate a row or column index, is used to access its items. Consider the C declaration int anArrayName[10], which declares a ten-instance one-dimensional array. The array can hold ten int entries in this case. The indices in this array range from zero to nine. The first and last elements, for example, are represented by the phrases anArrayName[0] and anArrayName[9].

What is sorting in java

Sorting is the process of arranging data in a preferred order in a data structure. Sorting data makes it easier to swiftly and simply search through it. A dictionary is the most basic example of sorting. In computer science, a variety of sorting methods are utilised.