1
votes

when I try to compile my file, I got these errors:

invalid method declaration; return type required public get(int i) throws SortingException{

invalid method declaration; return type required public set(int i, T elem) throws SortingException {

here are my methods:

public static <T extends Comparable<T>> ArrayList<T> insertionSort(ArrayList<T> list, int ordinamento){
    ArrayList<T> listaOrdenada;
    listaOrdenada = new ArrayList<T>(list);
    for(int x = 1 ; x < listaOrdenada.size(); x++){
      T aux = listaOrdenada.get(x);
      int y = x - 1;
      while(y >= 0 && aux.compareTo(listaOrdenada.get(y)) < 0){
        listaOrdenada.set(y + 1, listaOrdenada.get(y));
        y--;
      }
      listaOrdenada.set(y + 1, aux);
    }
    return listaOrdenada;
  }

public <T>  set(int i, T elem) throws SortingException {
    if(i<0 || i>=(this.array).size()) throw new SortingException("ERRORE!");
    return (this.array).set(i, elem);
  }

public <T>  get(int i) throws SortingException{
    if(i<0 || i>=(this.array).size()) throw new SortingException("ERRORE!");
    return (this.array).get(i);
  }
4

4 Answers

1
votes

You need to declare a return type for your methods.

public <T> void set(int i, T elem) throws SortingException {
    if(i<0 || i>=(this.array).size()) throw new SortingException("ERRORE!");
    return (this.array).set(i, elem);
}

public <T>  T get(int i) throws SortingException{
    if(i<0 || i>=(this.array).size()) throw new SortingException("ERRORE!");
    return (this.array).get(i);
}

Note that this assumes that your generic type should be bound on method level. It seems more likely that this should be done on class level, in which case you should have something like.

public class YourClassName <T extends Comparable<T>> {
    //Other methods and constructors goes here

    public void set(int i, T elem) throws SortingException {
        if(i<0 || i>=(this.array).size()) throw new SortingException("ERRORE!");
        return (this.array).set(i, elem);
    }

    public T get(int i) throws SortingException{
        if(i<0 || i>=(this.array).size()) throw new SortingException("ERRORE!");
        return (this.array).get(i);
    }
}
1
votes

Remove angle brackets from <T>:

public T set(...) // return type is the instance’s type for T
public T get(...) 

The syntax you used public <T> set typed the method with a type T that obscured the type of the class and which is typically used to declare a return type that varies with the call, ie public <T> T set(...) will return a type that is dependent on how the method is called (eg the type may be inferred) and that type will have nothing to do with the generic type of the instance on which it is called.

0
votes

Use return type

public <T> T set(...)
0
votes

As you declare :

public <T>  set(int i, T elem) throws SortingException {...}

you declare a scoped method type T. It doesn't define any return type.
So it is a compilation error.

While the following declares both a scoped method type T and this T as return type :

public <T> T set(int i, T elem) throws SortingException {...}

But defining a scoped method type is probably not the right way to solve your issue.
You don't show the whole class but it is very probably a bad idea to define scoped method generics as the generic type should be hold by the class as these generic methods work very probably with the same generic type.

Just remove the <T> in the method declaration and return T, that is the class generic :

public class MyClass<T>{

    public static ArrayList<T> insertionSort(ArrayList<T> list, int ordinamento){
      ...
    }

    public void set(int i, T elem) throws SortingException {
      ...
    }

    public T get(int i) throws SortingException{ 
      ...
    }

}