该代码只是用来比较盒式卷并按升序对它们进行排序。我使用junit来测试代码,当我尝试运行它时,我得到了这个结果。以前从未使用过堆栈溢出,所以如果这是发布问题的错误方式,我很抱歉。
终端信息
samin@Samins-MacBook-Air src % javac -cp ".:hamcrest-core-1.3.jar:junit-4.12.jar" BoxProcessorTest.java
BoxProcessorTest.java:131: error: no suitable method found for sort(List<Box>)
Collections.sort(expected);
^
method Collections.<T#1>sort(List<T#1>) is not applicable
(inference variable T#1 has incompatible bounds
equality constraints: Box
lower bounds: Comparable<? super T#1>)
method Collections.<T#2>sort(List<T#2>,Comparator<? super T#2>) is not applicable
(cannot infer type-variable(s) T#2
(actual and formal argument lists differ in length))
where T#1,T#2 are type-variables:
T#1 extends Comparable<? super T#1> declared in method <T#1>sort(List<T#1>)
T#2 extends Object declared in method <T#2>sort(List<T#2>,Comparator<? super T#2>)
BoxProcessorTest.java:203: error: no suitable method found for sort(List<Box>)
Collections.sort(evenList);
^
method Collections.<T#1>sort(List<T#1>) is not applicable
(inference variable T#1 has incompatible bounds
equality constraints: Box
lower bounds: Comparable<? super T#1>)
method Collections.<T#2>sort(List<T#2>,Comparator<? super T#2>) is not applicable
(cannot infer type-variable(s) T#2
(actual and formal argument lists differ in length))
where T#1,T#2 are type-variables:
T#1 extends Comparable<? super T#1> declared in method <T#1>sort(List<T#1>)
T#2 extends Object declared in method <T#2>sort(List<T#2>,Comparator<? super T#2>)
2 errors
这是实现comparable的box类,第二个是排序和查找特定box的类:
import java.util.Objects;
/**
* The Box class models a three-dimensional box
*/
public class Box implements Comparable<Box>{
private final int height;
private final int width;
private final int depth;
/**
* Create a new Box with the specified dimensions (height, width, depth).
*
* @param height the height of the box
* @param width the width of the box
* @param depth the depth of the box
*/
public Box(int height, int width, int depth) {
this.height = height;
this.width = width;
this.depth = depth;
}
/**
* Create a copy of box.
*
* @param box A Box to copy.
*/
public Box(Box box) {
this.height = box.height;
this.width = box.width;
this.depth = box.depth;
}
/**
* Get this box's volume
*
* @return the box's volume
*/
public int volume() {
return height * width * depth;
}
@Override
public int compareTo(Box o){
int x = ((Integer)this.volume()).compareTo(o.volume());
return x;
}
//....get height
//...get width
//....get depth
import java.util.List;
import java.util.Objects;
import java.lang.Math;
import java.util.ArrayList;
public class BoxProcessor {
public void sort(Box[] array){
for(int i = 0; i<=array.length-1;i++ ){
int min = 0;
for(int j = i+1; j<=array.length-1; j++){
if(array[j].compareTo(array[min]) < 0){
min = j;
Box temp = array[i];
array[i] = array[min];
array[min] = temp;
}
}
}
}
public void sort(List<Box> list){
for(int i = 0; i<=list.size()-1;i++ ){
int min = 0;
for(int j = i+1; j<=list.size()-1; j++){
if(list.get(j).compareTo(list.get(min)) < 0){
min = j;
Box temp = list.get(i);
list.set(i,list.get(min));
list.set(min,temp);
}
}
}
}
public int sequentialSearch(Box[] array, Box box){
for(int i = 0; i<=array.length-1; i++){
if(array[i].compareTo(box) == 0){
return i;
}
}
return -1;
}
public int sequentialSearch(List<Box> list, Box box){
for(int i = 0; i<=list.size()-1; i++){
if(list.get(i).compareTo(box) == 0){
return i;
}
}
return -1;
}
public int binarySearch(Box[] array, Box box){
int lowerBound = 0;
int upperBound = array.length -1;
while(lowerBound <= upperBound){
int index = (int)Math.floor((lowerBound-upperBound)/2);
if(array[index].compareTo(box) == 0){
return index;
}
else if(array[index].compareTo(box) < 0){
upperBound = index -1;
}
else if(array[index].compareTo(box) > 0){
lowerBound = index +1;
}
}
return -1;
}
public int binarySearch(List<Box> list, Box box){
int lowerBound = 0;
int upperBound = list.size() -1;
while(lowerBound <= upperBound){
int index = (int)Math.floor((lowerBound-upperBound)/2);
if(list.get(index).compareTo(box) == 0){
return index;
}
else if(list.get(index).compareTo(box) < 0){
upperBound = index -1;
}
else if(list.get(index).compareTo(box) > 0){
lowerBound = index +1;
}
}
return -1;
}
}
I have no idea why I'm getting this error. I've searched for the problem but didn't find any answers
1条答案
按热度按时间bqucvtff1#
t#1<?在方法<t#1>排序(list<t#1>)中声明的超级t#1>扩展了方法<t#2>排序(list<t#2>,comparator)中声明的对象?超级t#2>)
boxprocessortest。java:203:错误:找不到适用于排序(列表)集合的方法。排序(evenlist);
你有什么比较器吗?您需要将其更改为使用comparable