Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Reverse integer array program

Have Programming Question? Ask it here!
 
 
 


David Shipman
Reverse integer array program
1 Answer(s)      a year and 7 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
Reverse an array
Java NotesReverse an array This version of reverse uses two subscripts: one that starts at the left (beginning) of the array, and one that starts at the right (end) of the array. You can also use a for loop that goes
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
Two Dimensional array program
to elements of the array such that the last element become the first one and the first become the last.let the program output elements of the first array...Two Dimensional array program  consider a two dimensional array
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
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
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 arrays in java
reverse arrays in java  how do i write a program in array of size n*m where both n and m are greater than 20 such that the 1st element of an array becomes the last and vice verse
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    
reverse the charstring
reverse the charstring  how to reverse any character of the string
reverse arrays in java
reverse arrays in java  how do i make a code that can be used to reverse the elements of an array in two dimension such that the last element of an array becomes the first element of the array and the first element of an array
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
reverse two dimensional arrays in java
reverse two dimensional arrays in java  reverse array elements in two dimensional array such that the last element becomes the first
java 2 d array program
java 2 d array program  write a program 2-d matrix addition through user's input?  Hi Friend, Try the following code: import java.util.*; class MatrixAddition{ public static void main(String[] args
reverse string
reverse string  how to reverse a string without changing its place...=input.nextLine(); String reverse = new StringBuffer(str).reverse().toString(); System.out.println("Reverse: " + reverse); } } Thanks
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
reverse alphabet
reverse alphabet  e d c b a d c b a c b a b a a code for this pattern in c language
reverse indexing
reverse indexing  how to list out all the pages names that have keyword common in them when pages names and their respective keywords are given as input in text file format
integer to string
integer to string  i have to develop a program which convert integer into character from 1 to 10,000 if we input 1 then it give output 'one' n so on till 'ten thousand' kindly give me the codes plzplz sir........   Have
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
reverse albhabet
reverse albhabet   e d c b a d c b a c b a code for this pattern   Here is a code that displays the following pattern: e d c b a d c b a c b a b a a public
Advertisements
 

 

 

DMCA.com