How to resolve error: 15: error: cannot infer type arguments for PriorityQueue<> in openjdk 1.7.0_95?
How to resolve error: 15: error: cannot infer type arguments for PriorityQueue<> in openjdk 1.7.0_95?
我正在解决一个 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 中没有 PriorityQueue
构造函数只接受 Comparator
作为参数。看看 Java 7 Priority queue docs. However in Java 8+ there is such constructor 这个 class.
您最好的选择是使用具有初始容量和 Comparator
:
的构造函数
PriorityQueue<Integer> mx = new PriorityQueue<Integer>(10, Collections.reverseOrder());
该构造函数是在 java-8 中添加的,因此它无法在 1.7 中运行。
在 java-8 中添加了一个名为 target type 的功能,但这与您的问题无关;所以它就像再添加一个构造函数参数一样简单,例如 initial capacity
.
PriorityQueue<Integer> mx = new PriorityQueue<>(5, Collections.reverseOrder());
我正在解决一个 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 中没有 PriorityQueue
构造函数只接受 Comparator
作为参数。看看 Java 7 Priority queue docs. However in Java 8+ there is such constructor 这个 class.
您最好的选择是使用具有初始容量和 Comparator
:
PriorityQueue<Integer> mx = new PriorityQueue<Integer>(10, Collections.reverseOrder());
该构造函数是在 java-8 中添加的,因此它无法在 1.7 中运行。
在 java-8 中添加了一个名为 target type 的功能,但这与您的问题无关;所以它就像再添加一个构造函数参数一样简单,例如 initial capacity
.
PriorityQueue<Integer> mx = new PriorityQueue<>(5, Collections.reverseOrder());