Java不在列表中工作 公共类变量实现可比较{ 字符串名; 字符串类型; 公共变量(字符串名称、字符串类型){ this.name=名称; this.type=type; } 公共字符串getName(){ 返回名称; } 公共字符串getType(){ 返回类型; } 公共布尔等于(对象o){ 如果(o!=null&(o instanceof Variable)){ return((变量)o).getName().equals(名称)&& ((变量)o).getType().equals(类型); } 返回false; } 公共整数比较(变量v){ if(type.compareTo(v.type)!=0)返回type.compareTo(v.type); 返回name.compareTo(v.name); } 公共字符串toString(){ 返回类型+“”+名称; }

Java不在列表中工作 公共类变量实现可比较{ 字符串名; 字符串类型; 公共变量(字符串名称、字符串类型){ this.name=名称; this.type=type; } 公共字符串getName(){ 返回名称; } 公共字符串getType(){ 返回类型; } 公共布尔等于(对象o){ 如果(o!=null&(o instanceof Variable)){ return((变量)o).getName().equals(名称)&& ((变量)o).getType().equals(类型); } 返回false; } 公共整数比较(变量v){ if(type.compareTo(v.type)!=0)返回type.compareTo(v.type); 返回name.compareTo(v.name); } 公共字符串toString(){ 返回类型+“”+名称; },java,comparable,Java,Comparable,} 公共类操作实现可比较{ 字符串名; 字符串类型; 列出参数; 公共操作(字符串名称、字符串类型){ this.name=名称; this.type=type; 参数=新的ArrayList(); } 公共void addArgument(变量a){ 加入(a); } 公共字符串getName(){ 返回名称; } 公共字符串getType(){ 返回类型; } 公共列表getArguments(){ 返回参数; } public int getArgumentNumber(){ 返回参

}

公共类操作实现可比较{
字符串名;
字符串类型;
列出参数;
公共操作(字符串名称、字符串类型){
this.name=名称;
this.type=type;
参数=新的ArrayList();
}
公共void addArgument(变量a){
加入(a);
}   
公共字符串getName(){
返回名称;
}
公共字符串getType(){
返回类型;
}
公共列表getArguments(){
返回参数;
}
public int getArgumentNumber(){
返回参数。size();
}
公共布尔等于(对象o){
如果(o!=null&(o操作实例)){
List oa=((操作)o).getArguments();
Collection.sort(参数);
收集、分类(oa);
返回((操作)o).getName().equals(名称)&&
((操作)o.getType().equals(类型)&&
((操作)o).getArguments().equals(参数);
}
返回false;
}
公共内部比较(操作五){
if(type.compareTo(v.type)!=0)返回type.compareTo(v.type);
else if(name.compareTo(v.name)!=0)返回name.compareTo(v.name);
否则{
if(arguments.size()v.getArgumentNumber())返回1;
其他的
}
}
公共字符串toString(){
字符串s=“”;
if(arguments.isEmpty())返回类型+“”+name+“(“+”)”;
否则{
for(变量v:参数){
s+=v.type+“”;
}
}
返回类型+“”+name+”(“+s.trim()+”);
}
}

如您所见,在Operation类的equals函数中

public class Operation implements Comparable<Operation>{
String name;
String type;
List<Variable> arguments;   

public Operation(String name, String type){
    this.name = name;
    this.type = type;
    arguments = new ArrayList<Variable>();
}

public void addArgument(Variable a){
    arguments.add(a);
}   

public String getName(){
    return name;
}

public String getType(){
    return type;
}

public List<Variable> getArguments(){
    return arguments;
}

public int getArgumentNumber(){
    return arguments.size();
}

public boolean equals(Object o){
    if(o != null && (o instanceof Operation)) {
        List<Variable> oa = ((Operation)o).getArguments();
        Collection.sort(arguments);
        Collection.sort(oa);
        return ((Operation)o).getName().equals(name) &&
               ((Operation)o).getType().equals(type) &&
               ((Operation)o).getArguments().equals(arguments);
    }
    return false;
}

public int compareTo(Operation v){
    if (type.compareTo(v.type) != 0) return type.compareTo(v.type);
    else if(name.compareTo(v.name) != 0) return name.compareTo(v.name);
    else{
        if (arguments.size() < v.getArgumentNumber()) return -1;
        else if(arguments.size() > v.getArgumentNumber()) return 1;
        else 
    }
}

public String toString(){
    String s = "";
    if (arguments.isEmpty()) return type + " " + name + "(" +  ")";
    else {
        for(Variable v:arguments){
            s+= v.type + " ";
        }
    }
    return type + " " + name + "(" + s.trim() + ")";
}
公共布尔等于(对象o){
如果(o!=null&(o操作实例)){
List oa=((操作)o).getArguments();
Collection.sort(参数);
收集、分类(oa);
返回((操作)o).getName().equals(名称)&&
((操作)o.getType().equals(类型)&&
((操作)o).getArguments().equals(参数);
}
返回false;
}
我试图对变量列表进行排序,但它给了我错误消息“类型集合的方法排序(列表)未定义”,但我已经在变量类中定义了compareTo函数

任何人都知道我应该如何在操作类中定义compareTo函数,我应该先对参数排序,然后对每个变量进行比较吗


谢谢。

您使用了错误的类进行排序<代码>集合。排序不存在,但存在

公共布尔等于(对象o){
如果(o!=null&(o操作实例)){
List oa=((操作)o).getArguments();
集合。排序(参数);
收集.分类(oa);
返回((操作)o).getName().equals(名称)&&
((操作)o.getType().equals(类型)&&
((操作)o).getArguments().equals(参数);
}
返回false;
}

集合s,如复数形式,带“s”

有人知道我应该如何定义compareTo函数吗 操作类,我应该对第一个参数进行排序,然后对每个参数进行比较吗 变量

由于您在代码< >操作< /COD>类中依赖<代码>参数< /代码>,请考虑按<排序>顺序将<>代码>变量< /代码> s存储在<代码>参数< /代码>中。 这意味着使用每个新添加的

变量对
列表
进行排序,或者使用
集合
而不是
列表
。因此,您不必在
equals
compareTo
方法中进行排序

关于
compareTo
方法的问题: 如果
参数
的大小相同,则通过
compareTo
对两个
参数
列表进行迭代,比较
变量
的相同索引,并返回第一个结果,即
!=0
。(如果所有元素相等,则为“0”)

一种可能的解决办法:

public boolean equals(Object o){
    if(o != null && (o instanceof Operation)) {
        List<Variable> oa = ((Operation)o).getArguments();
        Collections.sort(arguments);
        Collections.sort(oa);
        return ((Operation)o).getName().equals(name) &&
               ((Operation)o).getType().equals(type) &&
               ((Operation)o).getArguments().equals(arguments);
    }
    return false;
}
public int compareTo(操作五){
int res=类型。比较(v.type);
如果(res!=0)返回res;
res=name.compareTo(v.name);
如果(res!=0)返回res;
if(arguments.size()!=v.arguments.size())返回arguments.size()-v.arguments.size();
对于(int i=0;i
您可能希望使用代码完整的优秀IDE。这种事情是基本的调试。请注意,添加到Java中的许多静态方法都位于无法实例化的类中。它们通常以
s
字符结尾,因为它们包含所有相关类和实例的方法。因此,对于
Collection
来说,您有
Collections
,它还包含
List
Set
等方法。类似地,还有
文件
,以及类似
标准字符集
的枚举。它应该是
Collections.sort
,而不是
Collection.sort
。另外,如果不重写
hashCode()
,请不要重写
equals()
。非常感谢,我使用Eclipse进行编码,它对集合有一个导入,这让我感到困惑。@Real怀疑如果我不重写hashCode(),会发生什么?@MaartenBodewes这就是我试图完成的。在完成集合。排序(参数)之后,我
public boolean equals(Object o){
    if(o != null && (o instanceof Operation)) {
        List<Variable> oa = ((Operation)o).getArguments();
        Collection.sort(arguments);
        Collection.sort(oa);
        return ((Operation)o).getName().equals(name) &&
               ((Operation)o).getType().equals(type) &&
               ((Operation)o).getArguments().equals(arguments);
    }
    return false;
}
public boolean equals(Object o){
    if(o != null && (o instanceof Operation)) {
        List<Variable> oa = ((Operation)o).getArguments();
        Collections.sort(arguments);
        Collections.sort(oa);
        return ((Operation)o).getName().equals(name) &&
               ((Operation)o).getType().equals(type) &&
               ((Operation)o).getArguments().equals(arguments);
    }
    return false;
}
public int compareTo(Operation v) {
  int res = type.compareTo(v.type);
  if (res != 0) return res;

  res = name.compareTo(v.name);
  if (res != 0) return res;

  if (arguments.size() != v.arguments.size()) return arguments.size() - v.arguments.size();

  for (int i = 0; i < arguments.size(); ++i) {
    res = arguments.get(i).compareTo(v.arguments.get(i));
    if (res != 0) return res;
  }

  return 0;
}