Sunday, 14 January 2018

Reverse of an array example in java

package com.tutorial4u;

import java.util.Arrays;

public class ArrayReverse
{
     public static void main(String[] args)
     {
          int i;
          int a[] = {1,14,5,17,4,2};
          for (i=0;i<a.length/2;i++)
          {
               int temp = a[i];
                   a[i]= a[a.length-1-i];
                   a[a.length-1-i]= temp;
              }
          System.out.println("Reverse Array is :"+Arrays.toString(a));
          }
     }


Output:

Reverse Array is :[2, 4, 17, 5, 14, 1]

No comments:

Post a Comment

No of Occurrence in Array

package com.tutorial4u; import java.util.HashMap; /************************************************************  No of Occurrence in Array...