-3

I have tried to invert an array using generic method in O(n) time complexity.But it is showing error of incompatible types for E and integer?can anybody correct? below is my code:

public class HelloWorld{
    static Integer intarray[]={0,1,2,3,5,6};  
    public static <E> void inverse(E inputArray[])
    {
        int i,low=0,hi=intarray.length-1;
        while(low<hi)
        {
            E temp=intarray[low];
            intarray[low]=intarray[hi];
            intarray[hi]=temp ;
            low++;hi--;
        }    

    }

    public static < E > void printArray( E[] inputArray )
   {
      for(E element : inputArray) {
         System.out.printf("%s ", element);
      }
      System.out.println();
   }

     public static void main(String []args)
     {
        //System.out.println("Hello World");

        printArray(intarray);
        inverse(intarray);


     }

}
  • 1
    `intarray` is an `int[]`, not an `E[]`. You probably meant to use `inputArray` instead. Voting to close as off-topic as your issue is down to a typo and thus unlikely to be useful to future visitors. – Joe C Jan 27 '18 at 14:17
  • @JoeC You're right, but intarray is an `Integer[]`, not an `int[]`, so it may come across in a confusing way. The `inverse` method should not refer to variable intarray but to `inputArray`. – Erwin Bolwidt Jan 27 '18 at 14:31
  • It's not "inverse", "converse", "traverse", and it's also not "transpose". It's called "reverse". – Andrey Tyukin Jan 27 '18 at 14:57

1 Answers1

1

You are using intarray instead of inputArray in the inverse function so the compiler gets that it's of Integer type and needs to be typecasted to E type. You just need to replace the intarray with inputArray to get the code working. The correct code will be-`

public class HelloWorld{
static Integer intarray[]={0,1,2,3,5,6};  
public static <E> void inverse(E inputArray[])
{
    int i,low=0,hi=inputArray.length-1;
    E temp;
    while(low<hi)
    {
        temp= inputArray[low];
        inputArray[low]=inputArray[hi];
        inputArray[hi]= temp ;
        low++;hi--;
    }    

}

public static < E > void printArray( E[] inputArray )
{
   for(E element : inputArray) {
      System.out.printf("%s ", element);
   }
   System.out.println();
}

 public static void main(String []args)
 {
    //System.out.println("Hello World");
    inverse(intarray); 
    printArray(intarray);



 }

}
Prateek Surana
  • 672
  • 9
  • 29