我想知道为什么这段代码会出现编译错误。我是 JAVA 新手。我想在 C++ 中制作一组对内部类代码中的对是什么意思
import java.io.*;
import java.util.Set;
public class UVa {
public static boolean flag = false;
public static Integer p0, p1;
public static Set<pair<Integer,Integer> > sorter;
public static class pair<first,second>{
public first First;
public second Second;
private pair(first First,second Second){
this.First = First;
this.Second = Second;
}
}
public static void main(String[] args) {
try {
while (true) {
p0 = System.in.read();
p1 = System.in.read();
sorter.add(pair<p0, p1>); //<<Syntax error on token ">", Expression expected after this token
}
} catch (IOException e) {
System.out.println(e.toString());
}
}
}
Run Code Online (Sandbox Code Playgroud)
看来您正在尝试构建一个新的pair使用
pair<p0, p1>
Run Code Online (Sandbox Code Playgroud)
但是,正确的语法是
new pair<Integer, Integer>(p0, p1)
Run Code Online (Sandbox Code Playgroud)