Reverse integer array program

Ads
 

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] 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};
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]);
         }
    }
}

Ads









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
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
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
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
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
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
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
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
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
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
one dimensional array program
one dimensional array program  Design and implement a java program that will read a file containing numbers and compute the following statistics: the rannge( low, high), the average and the median(middle number). Notes
program to implement array of objects
program to implement array of objects  import java.io.*; import java.util.*; public class dataa { DataInputStream in=new DataInputStream(System.in); int i; int a[i]=int acid[5]; String name1[i]=new String[5
Reverse
Reverse  How to reverse two digits in javaprogramming
Reverse
Reverse  How to reverse two digits in javaprogramming
Two Dimensional Array Program
a integer for array declaration Two dimensional array program. We are going... will learn how to display arrange form of two dimensional array program... Two Dimensional Array Program    
Two Dimensional Array Program
Two Dimensional Array Program Using Nested for loop...; program . In this session we will teach how to use of a two dimensional array. First of all we will define a class "Matrix". In this program we
Three Dimensional Array program
Three Dimensional Array program       This is a very simple Java program. In this program we... the multidimensional array we are going to make a matrix. Now in this program use
Write a java program that prints the decimal representation in reverse. (For example n=173,the program should print 371.)c
Write a java program that prints the decimal representation in reverse. (For example n=173,the program should print 371.)c  class rose { int n... { public static int reverse(int num){ int n = num; int
create and use an array variable in a java program
create and use an array variable in a java program  how do i write a program that will prompt the user for a list of 5 prices, once the user has entered all values , your program should compute and display the following: The sum
Two Dimensional Array Program Using Nested For Loop
Two Dimensional Array Program Using Nested For Loop ... dimensional array program. Firstly, we have to define class "TwoDimensional ". We are going to make a integer for array declaration Two dimensional array
array
array  write and test a function named mirror that is passed an array of n floats and returns a newly created array that contains those n floats values in reverse order . for example, the call of mirror function would transform
array
array  write and test a function named mirror that is passed an array of n floats and returns a newly created array that contains those n floats values in reverse order . for example, the call of mirror function would transform

Ads