我有两个List包含此类对象的 s:
public class SchoolObj
{
private String name;
private String school;
public SchoolObj()
{
this(null, null);
}
public SchoolObj(String nameStr, String schoolStr)
{
this.setName(nameStr);
this.setSchool(schoolStr);
}
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name = name;
}
public String getSchool()
{
return this.school;
}
public void setSchool(String school)
{
this.school = school;
}
@Override
public String toString()
{
return this.getName() + ' ' + this.getSchool();
}
}
Run Code Online (Sandbox Code Playgroud)
我想通过name和比较这两个列表中的对象school。如果它们相等,我需要创建一个 …
我正在写一个简单的程序如下:给定两个数字M和N,p来自[M,N],q来自[1,p-1],找到p/q的所有不可约分数.我的想法是蛮力所有可能的p,q值.并使用HashSet来避免重复的分数.但是,不知何故,contains函数没有按预期工作.
我的代码
import java.util.HashSet;
import java.util.Set;
public class Fraction {
private int p;
private int q;
Fraction(int p, int q) {
this.p = p;
this.q = q;
}
public static int getGCD(int a, int b) {
if (b == 0)
return a;
else
return getGCD(b, a % b);
}
public static Fraction reduce(Fraction f) {
int c = getGCD(f.p, f.q);
return new Fraction(f.p / c, f.q / c);
}
public static HashSet<Fraction> getAll(int m, int n) {
HashSet<Fraction> res = …Run Code Online (Sandbox Code Playgroud)