Developer community
0 votes
845 views
sir i am able to print the array in reverse order by using below code but i am not able to reverse the given array.
public class Reverse
{
    public static void main(String args[])
    {
        int a[]={1,2,3,4,5,6,7,8,9};
        for(int i=a.length-1;i>=0;i--)
        {
            System.out.println(a[i]);
        }
    }
}
asked by Geek (15.3k points) 14 39 60 | 845 views

1 Answer

+1 vote
Best answer

well try ..that is absolutely correct but we have to do some more  work... 

public class ReverseArray{
public static void main(String ar[]){
int array[]={5,4,1,7,9,6};
for(int i=array.length-1,j=0;i>array.length/2-1;i--,j++){
int temp=array[j];
array[j]=array[i];
array[i]=temp;
}
for(int k=0;k<array.length;k++)
System.out.print(array[k]+"  ");
}
}
answered by Expert (11.4k points) 14 22 28
selected by

Related questions

0 votes
1 answer
asked by sara Guru (24.6k points) 29 51 77 | 3.2k views
0 votes
1 answer
0 votes
1 answer
asked by augustaina Sr Member (4.8k points) 10 23 31 | 208 views
+1 vote
1 answer
asked by keem Expert (13.2k points) 27 40 57 | 34 views
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

Categories


C Books Guide and List
C++ Books Guide and List
Best Java Books

3.8k questions

4k answers

117 comments

10.7k users

3,751 questions
4,046 answers
117 comments
10,709 users