Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 比较<;关键、价值>;配对并排序_Java_Algorithm_Sorting_Compare_Keyvaluepair - Fatal编程技术网

Java 比较<;关键、价值>;配对并排序

Java 比较<;关键、价值>;配对并排序,java,algorithm,sorting,compare,keyvaluepair,Java,Algorithm,Sorting,Compare,Keyvaluepair,我有几对要分类的。我已经完成了比较实现,它似乎正在工作 我在网上也看到了一个解决方案,但问题是它不允许它是静态的 public static ArrayList <Pair<Integer, Integer>> pairList = new ArrayList<Pair<Integer, Integer>>(); public static Pair<Integer, Integer> pair1 = new Pair<

我有几对要分类的。我已经完成了比较实现,它似乎正在工作

我在网上也看到了一个解决方案,但问题是它不允许它是静态的

public static ArrayList <Pair<Integer, Integer>> pairList = new ArrayList<Pair<Integer, Integer>>();    

public static Pair<Integer, Integer> pair1 = new Pair<>(6, 7);
public static Pair<Integer, Integer> pair2 = new Pair<>(7, 7)

你可能称之为比较

compare(pair1,pair2);
pair1和pair2是静态的,不进行比较。将静态字段传递给函数时,函数也必须是静态的。这就是你犯这个错误的原因

您可以通过将比较方法更改为来解决此问题

public static int compare(Pair<Integer, Integer> o1, Pair<Integer, Integer> o2) {
    return o1.getMachineNo().compareTo(o2.getMachineNo());
}
公共静态int比较(对o1,对o2){
返回o1.getMachineNo().compareTo(o2.getMachineNo());
}
或字段到

public Pair<Integer, Integer> pair1 = new Pair<>(6, 7);
public Pair<Integer, Integer> pair2 = new Pair<>(7, 7);
公共对pair1=新对(6,7);
公共对pair2=新对(7,7);

但是你应该看看什么是“static”关键字以及它的作用。

听起来你好像不太了解static关键字的作用。这应该是您的第一步。一些代码似乎丢失了。你从哪里得到“错误”?@Gana,我不确定我是否理解了这个问题:你的意思是,如果我更改比较方法使其保持静态,它将不起作用?我们不能使用大写字母吗?不用担心,伙计们,我已经修复了它。通过从类中删除implements Comparator解决了该问题。
public Pair<Integer, Integer> pair1 = new Pair<>(6, 7);
public Pair<Integer, Integer> pair2 = new Pair<>(7, 7);