Can someone please help point out why I keep getting "bad operand types for binary operator <" ? The method takes in Lists of any numeric inputs for some operations. I am comparing two <? extends Number> objects here, and I guess error arises as not all Number subclasses impletement Comparable, so comparison cant be made. How can I solve the issue? --------------------------------------------- public static void remerge(List<? extends Number> A1, List<? extends Number> A2, List<? extends Number> A3){ int A1index = 0, A2index = 0, A3index = 0; while (A1index < A1.size() && A2index < A2.size()){ //while neither subarrays are exhausted if ( (A1.get(A1index)) < (A2.get(A2index)) ){ //error occurs A3.set(A3index,A1.get(A1index));
It is a merge sort method, below for Integer, trying to make it generic ------------------------------------------ public class MergeSort{ //sort a List or array in ascending order by merge sort public static void mergeSort(List<Integer> A){ if (A.size() > 1){ //stopping case List<Integer> A1 = new ArrayList<Integer>(A.subList(0, A.size()/2)); List<Integer> A2 = new ArrayList<Integer>(A.subList(A.size()/2, A.size())); mergeSort(A1); //recursively sort A1 mergeSort(A2); //recursively sort A2 remerge(A1, A2, A); //re-combine A1 and A2 into A as sorted } } public static void remerge(List<Integer> A1, List<Integer> A2, List<Integer> A3){ int A1index = 0, A2index = 0, A3index = 0; while (A1index < A1.size() && A2index < A2.size()){ //while neither subarrays are exhausted if (A1.get(A1index)<A2.get(A2index)){ //if A1 element is smaller than A2, move it to A3, else move A2 element to A3 A3.set(A3index,A1.get(A1index)); A3index++; A1index++;} else{ A3.set(A3index, A2.get(A2index)); A3index++; A2index++;} } //at this point either A1 or A2 is exhausted if (A1index<A1.size()){ //A1 is not exhausted, so A2 is while (A1index < A1.size()){ A3.set(A3index, A1.get(A1index)); A3index++; A1index++;}} else { while (A2index < A2.size()) { A3.set(A3index, A2.get(A2index)); A3index++; A2index++; } } } }
Some subclasses of Number don't have primitives and are not Comparable, such that <? extends Number> can't be compared by > and compareTo. Working now with simply <T extends Comparable>. ------------------------------------ import java.util.*; public class MergeSort{ //sort a List or array in ascending order by merge sort public static <T extends Comparable<T>> void mergeSort(List<T> A){ if (A.size() > 1){ //stopping case List<T> A1 = new ArrayList<T>(A.subList(0, A.size()/2)); List<T> A2 = new ArrayList<T>(A.subList(A.size()/2, A.size())); mergeSort(A1); //recursively sort A1 mergeSort(A2); //recursively sort A2 remerge(A1, A2, A); //re-combine A1 and A2 into A as sorted } } public static <T extends Comparable<T>> void remerge(List<T> A1, List<T> A2, List<T> A3){ int A1index = 0, A2index = 0, A3index = 0; while (A1index < A1.size() && A2index < A2.size()){ //while neither subarrays are exhausted if ( ((A1.get(A1index)).compareTo(A2.get(A2index))) < 0 ){ //if A1 element is smaller than A2, move it to A3, else move A2 element to A3 A3.set(A3index,A1.get(A1index)); A3index++; A1index++;} else{ A3.set(A3index, A2.get(A2index)); A3index++; A2index++;} } //at this point either A1 or A2 is exhausted if (A1index<A1.size()){ //A1 is not exhausted, so A2 is while (A1index < A1.size()){ A3.set(A3index, A1.get(A1index)); A3index++; A1index++;}} else { while (A2index < A2.size()) { A3.set(A3index, A2.get(A2index)); A3index++; A2index++; } } } }
This is indeed one possibility. Another possibility is to consider whether you really need such a general approach, or whether you can use lists which are more limited (e.g. to integers, or doubles only).
I guess you're coming from C++ and trying to use operator overloading, there's no such thing in Java. Anyways, don't reinvent the wheel, use java.util.Collections.sort().
Can someone inform why I get Static Error: 'no method in Stream<Interger> with name 'collect' matches this invocation' ? https://docs.oracle.com/javase/8/do...ream.html#collect-java.util.stream.Collector- --------------------------------------------------------------------- > import java.util.*; > import java.util.stream.*; > int[] number = new int[]{1, 2, 3}; List<Integer> list = Arrays.stream(number).boxed().collect(Collectors.toList()); //error List<Integer> list = IntStream.of(number).boxed().collect(Collectors.toList()); //error too
Thanks. It works now after I compile. was testing things in Interactions pane and didnt work there. Seems Interactions doesn't have static stuff, hence the static call error.