David Shipman
Reverse integer array program
1 Answer(s)      2 years and 5 months ago
Posted in : Java Beginners

Been tasked with the following question:

Write a method that accepts an integer array and returns a new array with all the elements in reverse order. For example, if the input array is [2, 4, 6, 8] the output array is [8, 6, 4, 2]. Then call your method from the main method.

I have this so far:

public static int [] reverse (int a[]){
        for ( int i = 0 ; i < a.length/2 ; i++ ){
        int temp = a[i] ;
        a[i] =  a[a.length-i-1] ;
        a[a.length-i-1] = temp ;

        }
        return a;
    }

    public static void main(String[] args) {
        int a [] = {2,4,6,8};
Advertisement
View Answers

December 7, 2012 at 11:48 AM


Here is an example of reversing an integer array.

import java.util.*;
class ReverseArray{
    public static int [] reverse (int a[]){
        for ( int i = 0 ; i < a.length/2 ; i++ ){
        int temp = a[i] ;
        a[i] =  a[a.length-i-1] ;
        a[a.length-i-1] = temp ;

        }
        return a;
    }

    public static void main(String[] args) 
    {
        int a[]=new int[5];
         Scanner input=new Scanner(System.in);
         System.out.println("Enter five array elements: ");
         for(int i=0;i<a.length;i++){
             a[i]=input.nextInt();
         }
         int array[]=reverse(a);
         System.out.println("Array In Reverse Order: ");
         for(int i=0;i<array.length;i++){
             System.out.println(array[i]);
         }
    }
}









Related Tutorials/Questions & Answers:
Reverse integer array program
Reverse integer array program  Been tasked with the following question: Write a method that accepts an integer array and returns a new array with all the elements in reverse order. For example, if the input array is [2, 4, 6, 8
Java Reverse integer array
Java Reverse integer array In this tutorial, you will learn how to reverse integer array. Here, we have created a method to reverse an array which has been passed as a parameter and returns a new array with all the elements in reverse
Advertisements
String reverse program
String reverse program  write a java program for reverse string? if the string is "Hi welcome to hyderabad" the reverse string is "hyderabad... for help. You can split the String into String array using split method of String
PHP Array Sort in Reverse Order
PHP Array Sort in Reverse Order  Hi, I have just started learning... the PHP Array Sort in Reverse Order. Please suggest any online reference or example... the Reference example of PHP Array Sort in Reverse Order. Thanks
JavaScript array reverse example
JavaScript array reverse example       JavaScript Array class have one method reverse() which will reverse the order of element into the array. This has a simple syntax
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
A Program To Reverse Words In String in Java .
A Program To Reverse Words In String in Java .  A Program To Reverse Words In String in Java :- For Example :- Input:- Computer Software Output :- Software Computer
ARRAY REVERSE - Java Interview Questions
ARRAY REVERSE  Hi,I Have Array Like This int arr[]={1,3,4,6,7,9,6,4} I Want Print Reverse Order Like This 4,6,9,7,6,4,3,1 Using loops I Want Source Code Plz Help me?  Hi Friend, Try the following code: class
A Program To Reverse Words In String in Java
A Program To Reverse Words In String in Java  A Program To Reverse... Tell Me This Program.  Here is an example that reverse the words...;i++){ if(i==ch.length){ reverse(ch,index,i-1
quiz asked by my lecturer for array reverse....
quiz asked by my lecturer for array reverse....  consider a 2 dimensional array size m by n.Derive a function that can be used to reverse the elements of the array such that the last element of the array becomes the first
PHP Array Reverse Sorting
PHP Array Sort in Reverse Order In PHP there are three functions are available... of the function then it will be clear that this function will sort an array in reverse... ) After sorting the keys in reverse order: Array ( [30] => twitter [11] =>
PHP Array Reverse
Php array is reversed by the function array_reverse(). array_reverse.... PHP Array Reverse Example <?php     $arr=array("car... are <br>";     $arr1=array_reverse($arr);    
quiz asked by my lecturer for array reverse....
quiz asked by my lecturer for array reverse....  consider a 2 dimensional array size m by n.Derive a function that can be used to reverse the elements of the array such that the last element of the array becomes the first
program of array
program of array  write a program that initializes an array with ten random integers and then prints four lines of output,containing:every element at an even index,every even element,all elements in reverse order,and only
Reverse String Program in Java
Reverse String Program in Java We are going to describe about reverse string program in java. In this example reverses a string entered by the user. We... insertion sort with string array A program to find the largest word
how to check and print the no of times an integer is repeated in an array
how to check and print the no of times an integer is repeated in an array  how to check and print the no of times an integer is repeated in an array??? suppose my input is 2 4 2 5 2 4 3 4 5 2 then my output should be 2-4
program in array
program in array  print("code sample");write a program that initializes an array with ten random integers and then prints four lines of output,containing:every element at an even index,every even element,all elements in reverse
Reverse
Reverse  How to reverse two digits in javaprogramming
Reverse
Reverse  How to reverse two digits in javaprogramming
array program
array program  write a java program which will take 10 elements as command line arguments and count how many times 3 occurs in array
reverse
reverse  program to read a string and print the reverse string   Hi Friend, Try the following code: import java.util.*; public class ReverseString{ public static void main(String[]args){ Scanner input=new
java program (integer/decimal) - Java Beginners
java program (integer/decimal)  Accept a number from the user as command line arguments. The number could be an integer or decimal value. ? Display the absolute value of the input number ? Display the rounded off value
Out of bounce exception in the string reverse program - Java Beginners
have any confuse then you can this program. i am sending reverse program code...Out of bounce exception in the string reverse program  In the given program below this is taken from ur site but when i am executing it.It is giving
how to make a program on array
how to make a program on array  When you make a program on array that the element will move downward and upward and when you input twice 0 then thats the time that it will not move. pls. give me a formula...tnx
java program based on array
java program based on array  write a program to create an array of 10 integers in main.accept values from the user in that array .now again ask the use another nomber and pass the array and the no. entered to function called
array variable in a java program
array variable in a java program  How do i write a program that will prompt the user for a list of 5 prices, that cosist of the sum of all the prices, the average of the prices, and all prices that are higher than the calculated
Program to implement array of Objects.
Program to implement array of Objects.  Create a class it has.... Initialize an array of objects. Pass a name from the command line & search for the name in the array of objects , if it is existing print the information
java program based on array
java program based on array  Create Student class with attributes... StudentDemo which will have main method. Declare array of Student object with size 10. Initialize all 10 elements of this array with new objects. Write logic which