4

I run into a problem that I do not understand. In the method of adding in the Table class I would like to use:

Arrays.binarySearch(asso, (a,b) -> a.cle.compareTo(b.cle));

Where asso is my array of Association objects containing the key and the value.

By executing this code with the type String, I get an error "cannot find symbol" on the method compareTo(Object).

There my code:

test:

Table<String,String> table0 = new Table<String,String>(10);
table0.add("1","int");

method:

import java.util.Arrays;

public class Table<C,V>{

  Association[] asso;

  public Table(int n){
    asso = new Association[n];
  }

  public void add(C cle, V valeur){
    asso[0] = new Association<C,V>(cle,valeur);
    Arrays.sort(asso, (a,b) -> a.cle.compareTo(b.cle));
  }

  public class Association<C,V>{

    public C cle;
    public V valeur;

    public Association(C cle,V valeur){
      this.cle = cle;
      this.valeur = valeur;
    }
  }
}

And the error:

../src/Table.java:15: error: cannot find symbol
Arrays.sort(asso, (a,b) -> a.cle.compareTo(b.cle));
                                ^
symbol:   method compareTo(Object)
location: variable cle of type C
where C is a type-variable:
C extends Object declared in class Table.Association

Thank you for your help.

1 Answer 1

3

Add a type bound to the generic type parameter C:

class Table<C extends Comparable<C>,V>

Otherwise the compiler doesn't know that C implements Comparable (and therefore must have the compareTo method).

Also don't use raw type (Association[]) for the array. Use:

Association<C,V>[] asso;

On second thought, I also suggest you make the Association class static:

public class Table<C extends Comparable<C>,V> {

    Association<C,V>[] asso;

    public Table(int n) {
        asso = new Association[n];
    }

    public void add(C cle, V valeur) {
        asso[0] = new Association<C,V>(cle,valeur);
        Arrays.sort(asso, (a,b) -> a.cle.compareTo(b.cle));
    }

    public static class Association<C,V> {

        public C cle;
        public V valeur;

        public Association(C cle,V valeur){
            this.cle = cle;
            this.valeur = valeur;
        }
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

i have done class Association<C extends Comparable<C>,V> class Table<C extends Comparable<C>,V> and now i have the error: Exception in thread "main" java.lang.NullPointerException at Table.lambda$ajout$0(Table.java:15) at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355) at java.util.TimSort.sort(TimSort.java:220) at java.util.Arrays.sort(Arrays.java:1438) at Table.ajout(Table.java:15) at main.main(main.java:546)
@takesuma The NullPointerException is a separate issue. You are trying to sort an array that has 1 Association instance and 9 null references. Besides, your add method always assigns to the first index of the array, which can't be right.
@takesuma yes, unless you change your lambda expression to handle the case where a or b are null.
I tried this: Arrays.sort(asso, (@NotNull C a,@NotNull C b) -> a.cle.compareTo(b.cle)); but it don't work it's my first use of lambda so i don't know well how it works.
@takesuma just use Arrays.sort(asso, Comparator.nullsLast(Comparator.comparing (Association::getCle))); (and add a getter method getCle() to Association class).
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.