成对定义 if 语句条件,并且仅在一个对象的一个参数上
Define if statement conditions by pairs, and on only one parameter of an object
我使用 jgrapht 库创建了一个有向图,我的顶点是 Point
个对象:
public static class Point {
public int x;
public int y;
public String type;
public Point(int x, int y, String type)
{
this.x = x;
this.y = y;
this.type = type;
}
@Override
public String toString() {
return ("[x="+x+" y="+y+" type="+type+ "]");
}
@Override
public int hashCode() {
int hash = 7;
hash = 71 * hash + this.x;
hash = 71 * hash + this.y;
return hash;
}
@Override
public boolean equals(Object other)
{
if (this == other)
return true;
if (!(other instanceof Point))
return false;
Point otherPoint = (Point) other;
return otherPoint.x == x && otherPoint.y == y;
}
}
我可以像这样遍历所有顶点并获得每个点的后继点:*
for (Point myPoint : directedGraph.vertexSet ()) {
for (Point successor : Graphs.successorListOf (directedGraph, myPoint)){
// Add if statement
}
}
我需要成对定义 if 语句的条件,但只能针对一个或两个 Point 参数。例如,这些是我的观点:
public static Point startPoint = new Point(2, 6, "A");
public static Point firstPoint = new Point(2, 7, "A");
public static Point secondPoint = new Point(2, 8, "B");
public static Point thirdPoint = new Point(2, 9, "B");
public static Point fourthPoint = new Point(2, 10, "B");
public static Point fifthPoint = new Point(3, 7, "C");
public static Point sixthPoint = new Point(4, 7, "C");
public static Point seventhPoint = new Point(5, 7, "C");
我想要一个条件来确保 myPoint 和 successor 没有特定的 "type":
for (Point myPoint : directedGraph.vertexSet ()) {
for (Point successor : Graphs.successorListOf (directedGraph, myPoint)){
if ((myPoint,successor) != (myPoint.type.equals("A"), successos.type.equals("B") && (myPoint,successor)!= (myPoint.type.equals("A"), successos.type.equals("C")){
// Do what I want
}
}
}
所以我需要在几个点上设置条件,但只在我的 Point
对象的一个参数上设置条件 [这对(myPoint 和 successor)不能分别(类型A 和 B 型)和(A 型和 C 型)。
这样可以吗?我找不到这方面的任何资源(并且在表达我的问题时遇到了问题,所以它没有帮助)
调查javatuples you could make your own as described in this post
但它可能看起来不漂亮。你会做类似的事情,
//define your tuple class
class myTuple
{
final String T1;
final String T2;
public myTuple(String t1, String t2)
{
T1 = t1;
T2 = t2;
}
if(myTupple(myPoint.type, successos.type) == myTupple("A", "B"))
{ /* do something */ }`
无论如何,我不认为你可以把它做得很漂亮,但你明白了。
我要做的是向 class 添加一个名为 "validateType" 或其他任何东西的静态方法,然后将这两个点传递给您在那里的检查
我使用 jgrapht 库创建了一个有向图,我的顶点是 Point
个对象:
public static class Point {
public int x;
public int y;
public String type;
public Point(int x, int y, String type)
{
this.x = x;
this.y = y;
this.type = type;
}
@Override
public String toString() {
return ("[x="+x+" y="+y+" type="+type+ "]");
}
@Override
public int hashCode() {
int hash = 7;
hash = 71 * hash + this.x;
hash = 71 * hash + this.y;
return hash;
}
@Override
public boolean equals(Object other)
{
if (this == other)
return true;
if (!(other instanceof Point))
return false;
Point otherPoint = (Point) other;
return otherPoint.x == x && otherPoint.y == y;
}
}
我可以像这样遍历所有顶点并获得每个点的后继点:*
for (Point myPoint : directedGraph.vertexSet ()) {
for (Point successor : Graphs.successorListOf (directedGraph, myPoint)){
// Add if statement
}
}
我需要成对定义 if 语句的条件,但只能针对一个或两个 Point 参数。例如,这些是我的观点:
public static Point startPoint = new Point(2, 6, "A");
public static Point firstPoint = new Point(2, 7, "A");
public static Point secondPoint = new Point(2, 8, "B");
public static Point thirdPoint = new Point(2, 9, "B");
public static Point fourthPoint = new Point(2, 10, "B");
public static Point fifthPoint = new Point(3, 7, "C");
public static Point sixthPoint = new Point(4, 7, "C");
public static Point seventhPoint = new Point(5, 7, "C");
我想要一个条件来确保 myPoint 和 successor 没有特定的 "type":
for (Point myPoint : directedGraph.vertexSet ()) {
for (Point successor : Graphs.successorListOf (directedGraph, myPoint)){
if ((myPoint,successor) != (myPoint.type.equals("A"), successos.type.equals("B") && (myPoint,successor)!= (myPoint.type.equals("A"), successos.type.equals("C")){
// Do what I want
}
}
}
所以我需要在几个点上设置条件,但只在我的 Point
对象的一个参数上设置条件 [这对(myPoint 和 successor)不能分别(类型A 和 B 型)和(A 型和 C 型)。
这样可以吗?我找不到这方面的任何资源(并且在表达我的问题时遇到了问题,所以它没有帮助)
调查javatuples you could make your own as described in this post
但它可能看起来不漂亮。你会做类似的事情,
//define your tuple class
class myTuple
{
final String T1;
final String T2;
public myTuple(String t1, String t2)
{
T1 = t1;
T2 = t2;
}
if(myTupple(myPoint.type, successos.type) == myTupple("A", "B"))
{ /* do something */ }`
无论如何,我不认为你可以把它做得很漂亮,但你明白了。
我要做的是向 class 添加一个名为 "validateType" 或其他任何东西的静态方法,然后将这两个点传递给您在那里的检查