gpt4 book ai didi

java - 如何解决错误 : 15: error: cannot infer type arguments for PriorityQueue<> in openjdk 1. 7.0_95?

转载 作者:搜寻专家 更新时间:2023-10-31 19:51:30 26 4
gpt4 key购买 nike

我正在解决 question on hackerearth当我使用 Java 版本 Java 8 (oracle 1.8.0_131) 时,它成功编译并通过了,但是当使用 Java (openjdk 1.7.0_95) 时,它给出了 error 15: error: cannot infer type arguments for PriorityQueue<> .错误是上线时mx正在声明优先级队列。我想知道如何解决它以及为什么会出现此错误。这是代码:(请注意,这个问题不是任何正在进行的比赛的一部分)并且代码的相关部分仅在主要功能中。

import java.io.*;
import java.util.*;

class TestClass {

public static void main(String[] args) {
InputReader sc = new InputReader(System.in);
int Q=sc.nextInt();
PriorityQueue<Integer> mn=new PriorityQueue<>();
PriorityQueue<Integer> mx=new PriorityQueue<>(Collections.reverseOrder());
int[] cnt =new int[100000+1];
for (int q = 0; q < Q; q++) {
String str=sc.nextLine();
if(str.substring(0,4).equals("Push")) {
int X=Integer.parseInt(str.substring(5));
++cnt[X];
mx.add(X);
mn.add(X);
}
else if (str.equals("Diff")) {
if(mx.isEmpty()||mn.isEmpty())
out.println(-1);
else {
int min = mn.poll();
int max = mx.poll();
if(min==max) {
--cnt[max];
}
else {
--cnt[min];
--cnt[max];
}
mn.remove(max);
mx.remove(min);
out.println(max-min);
}
}
else if (str.equals("CountHigh")) {
if(mx.isEmpty()) {
out.println(-1);
}
else {
out.println(cnt[mx.peek()]);
}
}
else {
if(mn.isEmpty()) {
out.println(-1);
}
else {
out.println(cnt[mn.peek()]);
}
}
// System.out.println(q+" "+mx+" "+mn);
}

out.close();
}
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static int mod = 1000000000+7;
static class InputReader {

private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;

public InputReader(InputStream stream) {
this.stream = stream;
}

public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}

public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}

public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}

public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}

public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}

public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}

public double nextDouble() {
return (Double.parseDouble(readString()));
}

public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}

private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}

public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}

最佳答案

在 Java 7 中,没有仅将 Comparator 作为参数的 PriorityQueue 构造函数。看看Java 7 Priority queue docs .但是在 Java 8+ 中有这样的 constructor对于这门课。

您最好的选择是使用具有初始容量和 Comparator 的构造函数:

PriorityQueue<Integer> mx = new PriorityQueue<Integer>(10, Collections.reverseOrder());

关于java - 如何解决错误 : 15: error: cannot infer type arguments for PriorityQueue<> in openjdk 1. 7.0_95?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57242140/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com