search
Login

Categories


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

3.8k questions

4k answers

117 comments

10.7k users

0 votes
828 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 Geek 15.3k points 14 39 60

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 Expert 11.4k points 14 22 28
selected by

Related questions

0 votes
1 answer 3.2K views
3.2K views
asked sara Guru 24.6k points 29 51 79
0 votes
1 answer 39 views
0 votes
1 answer 210 views
210 views
asked augustaina Sr Member 4.8k points 10 25 31
1 vote
1 answer 34 views
34 views
What's the simplest way of printing an array of primitives or of objects in Java? Here are some example inputs and outputs: int[] intArray = new int[] {1, 2, 3, 4, 5}; //output: [1, 2, 3, 4, 5] String[] strArray = new String[] {"John", "Mary", "Bob"}; //output: [John, Mary, Bob]
asked keem Expert 13.2k points 26 40 57
...