In the previous article, we have seen Java Program to Copy an Array to Another Array
In this article we are going to see how we can copy an array in reverse.
Java Program to Copy an Array in Reverse
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 copy an array in reverse.
Method-1: Java Program to Copy an Array in Reverse By Static Initialization of Array Elements
Approach:
- Create and initialize an array.
- Display the array to the user.
- Copy each element from the last location to the first location and store it in our copied array.
- Display the copied array.
Program:
import java.util.Arrays; import java.util.Collections; public class Main { public static void main(String args[]) { //Original array int arr[] = {12, 22, 34, 22, 54}; int copyarr[] = new int[arr.length]; // Printing the array System.out.println("The array elements are : "+Arrays.toString(arr)); // Copying each element from the array for(int i = 0;i<arr.length;i++) copyarr[i] = arr[arr.length-i-1]; System.out.println("The copied array elements are : "+Arrays.toString(copyarr)); } }
Output: The array elements are : [12, 22, 34, 22, 54] The copied array elements are : [54, 22, 34, 22, 12]
Method-2: Java Program to Copy an Array in Reverse By Dynamic Initialization of Array Elements
Approach:
- Create and initialize an array.
- Display the array to the user.
- Copy each element from the last location to the first location and store it in our copied array.
- Display the copied array.
Program:
import java.util.*; public class Main { public static void main(String args[]) { Scanner scan = new Scanner(System.in); // Taking size as input from the user System.out.println("Enter the array size :"); int size = scan.nextInt(); // Creating the array int arr[] = new int[size]; // Entering the array elements System.out.println("Enter array elements : "); for(int i=0;i<size;i++) { arr[i] = scan.nextInt(); } int copyarr[] = new int[arr.length]; // Printing the array System.out.println("The array elements are : "+Arrays.toString(arr)); // Copying each element from the array for(int i = 0;i<arr.length;i++) copyarr[i] = arr[arr.length-i-1]; System.out.println("The copied array elements are : "+Arrays.toString(copyarr)); } }
Output: Enter the array size : 6 Enter array elements : 1 2 3 4 5 6 The array elements are : [1, 2, 3, 4, 5, 6] The copied array elements are : [6, 5, 4, 3, 2, 1]
Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews.
Related Java Programs: