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

Related questions

0 votes
1 answer 3,122 views
0 votes
1 answer 35 views
0 votes
1 answer 199 views
199 views asked by augustaina Sr Member (4,820 points)
+1 vote
1 answer 32 views
32 views asked by keem Expert (13,240 points)
...