I am getting this error, "The method mergeSort(java.util.ArrayList) in the type Preferences is not applicable for the arguments (java.util.ArrayList)Java(67108979)" on my code ( I will paste below). But to me it looks like the exact same method so I am confused as to what is wrong.
The error occurs in preferenceSimilarity on the two lines where I try to call mergeSort.
import java.util.*;
public class Preferences<T>{
//Combine the two ArrayLists
private ArrayList<T> merge(ArrayList<T> left, ArrayList<T> right){
ArrayList<T> result= new ArrayList<T>();
int i=0; int j=0;
while ((i< left.size()) && (j<right.size())){
if (left.get(i).equals(right.get(j))){
result.add(left.get(i));
i++;
j++;
}
else if(left.get(i).hashCode()< right.get(j).hashCode()){
i++;
}
else{
j++;
}
}
return result;
}
//MERGE SORT
public ArrayList<T> mergeSort(ArrayList<T> list1){
if (list1.size()<=1){
return list1;
}
int mid= list1.size()/2;
ArrayList<T> left= new ArrayList<T>(list1.subList(0,mid));
ArrayList<T> right = new ArrayList<T>(list1.subList(mid,list1.size()));
left= mergeSort(left);
right= mergeSort(right);
return merge(left, right);
}
public static <T> double preferenceSimilarity(ArrayList<T> list1, ArrayList<T> list2){
** ArrayList<T> sorted1= mergeSort((ArrayList<T>) list1); //INCOMPATIBLE TYPES??
ArrayList<T> sorted2= mergeSort((ArrayList<T>) list2);**
double similarity= 0.0;
int i=0;
int j=0;
while (i < sorted1.size() && j < sorted2.size()) {
if (sorted1.get(i).equals(sorted2.get(j))) {
similarity++;
i++;
j++;
} else if (sorted1.get(i).hashCode() < sorted2.get(j).hashCode()) { //HashCode returns integer value
i++;
} else {
j++;
}
}
double totalLen= Math.max(sorted1.size(),sorted2.size());
return (similarity/totalLen);
}
}
I tried to type cast them and that did nothing. I also tried to switch around my argument types but that didn't work because I need my solution to handle the generic types.
As the commenters have said, this is because you are calling a non-static from a static. Sadly that is not the error message, probably because of the generic type.
Create an instance as follows and call the methods non-statically:
public static <T> double preferenceSimilarity(ArrayList<T> list1, ArrayList<T> list2){
Preferences<T> preferencesInstance = new Preferences<T>();
ArrayList<T> sorted1= preferencesInstance.mergeSort((ArrayList<T>) list1);
ArrayList<T> sorted2= preferencesInstance.mergeSort((ArrayList<T>) list2);