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

3,749 questions

4,049 answers

114 comments

10,705 users

how to reverse an array?

0 votes
765 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 Aug 9, 2014 by smith Geek (15,340 points)

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 Aug 9, 2014 by sunny Expert (11,410 points)
selected Aug 9, 2014 by smith

Related questions

0 votes
1 answer 3,089 views
0 votes
1 answer 34 views
34 views asked Jan 5, 2014 by anonymous
0 votes
1 answer 188 views
188 views asked May 14, 2014 by augustaina Sr Member (4,820 points)
+1 vote
1 answer 31 views
31 views asked Jan 24, 2014 by keem Expert (13,240 points)
...