[英]java arraylist contains type on object
I have an object Edges which has 5 different integer types associated with the object. 我有一个对象Edges,它具有与该对象关联的5个不同的整数类型。 like this:
像这样:
public class Edge implements Comparable {
int weight, tox, toy, fromx, fromy;
public Edge(int x1, int y1, int x2, int y2, int wei) {
tox = x1;
toy = y1;
fromx = x2;
fromy = y2;
weight = wei;
}
public int compareTo(Object obj) {
I want to make an arraylist of these objs and then call contains on the list to see if an arbitrary integer is in either the list at any of these data types, edge.tox edge.toy edge.fromx or edge.fromy .... is there a way to do this? 我想创建这些objs的arraylist,然后在列表上调用contains,以查看任意整数是否在这些数据类型(edge.tox edge.toy edge.fromx或edge.fromy)的列表中。 。 有没有办法做到这一点?
thank you in advanced 谢谢高级
Add this method to the Edge
class 将此方法添加到
Edge
类
public boolean contains(int num) {
if(tox == num) return true;
if(fromx == num) return true;
if(toy == num) return true;
if(fromy == num) return true;
return false;
}
Then you can just do: 然后,您可以执行以下操作:
public Edge getEdgeFromNumber(int number) {
for(Edge e : myArrayList) {
if(e.contains(number)) return e;
}
return null; // there are no such edges
}
Also, don't use the raw type Comparable
, you probably want Comparable<Edge>
另外,不要使用原始类型
Comparable
,您可能需要Comparable<Edge>
Use this: 用这个:
public List<Integer> asList() {
Arrays.asList(weight, tox, toy, fromx, fromy);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.