Menu
Coddy logo textTech

Recap - Reversed Array

Part of the Fundamentals section of Coddy's Java journey — lesson 63 of 73.

challenge icon

Challenge

Easy

Write a method named reverse which gets an array of numbers as argument and returns the reversed array.

For example, for [1, 2, 3], the expected output is [3, 2, 1].

Try it yourself

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

public class Main {
    public static int[] reverse(int[] arr) {
        // Write your code below
       
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String text = scanner.nextLine();
        String[] stringArr = text.split(",");
        int[] arr = new int[stringArr.length];
        for (int i = 0; i < stringArr.length; i++) {
            arr[i] = Integer.parseInt(stringArr[i]);
        }
        
        int[] result = reverse(arr);
        System.out.println("The reversed array is: " + Arrays.toString(result));
    }
}

All lessons in Fundamentals