ProAnswers.org

How to reverse an array?

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

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

System.out.print(array[k]+"  ");

}

}