Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.Scanner;
- import java.util.Arrays;
- public class Main {
- public static void main(String[] args) {
- inputArray();
- }
- public static void inputArray() {
- Scanner scan = new Scanner(System.in);
- System.out.println("Enter array size:");
- int arraySize = scan.nextInt();
- int[] array = new int[arraySize];
- for (int i = 0; i < array.length; i++) {
- System.out.println("Enter array value " + (i+1));
- array[i] = scan.nextInt();
- }
- reverse(array);
- }
- public static void reverse(int[] array) {
- int[] tempArray = new int[array.length];
- System.out.println("Array before reverse: " + Arrays.toString(array));
- for (int i = 0; i < array.length; i++) {
- tempArray[i] = (array.length - i);
- }
- array = tempArray;
- System.out.println("Array after reverse: " + Arrays.toString(tempArray));
- }
- }
- // write a method called reverse() with an int array as a parameter
- // The method should not return any value. The method can modify the array parameter
- // In main() test the reverse() method and print the array both in reversed and non-reversed order
- // To reverse an array, we need to swap the position of the elements
- // For example, an array of {1 5 9 9 2} would become {2 9 9 5 1}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement