简体   繁体   English

java arraylist包含对象的类型

[英]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);
}

Java 方法添加 ArrayList 未定义类型<object><div id="text_translate"><p>我正在为我的作业构建一个 java 程序,我必须将产品添加到特定商店。 尝试从 Store class 添加到 ArrayList 时遇到问题。</p><p> 我有 class 产品如下:</p><pre> class Product { private String pName; private int pPrice; private int pQty; public Product (String pName, int pPrice, int pQty) { this.pName = pName; this.pPrice = pPrice; this.pQty = pQty; } }</pre><p> class 存储如下:</p><pre> class Store { private String storeName; ArrayList&lt;Product&gt; pList =new ArrayList&lt;&gt;(); public Store() { String name = storeName; pList = new ArrayList&lt;Product&gt;(); } public Store(String newStoreName,ArrayList&lt;Product&gt; newPList) { this.storeName = newStoreName; this.pList = newPList; } void setName(String storeName) { this.storeName = storeName; } void setProduct(Product pList) { pList.add(this.pList);//This return method add undefined for type Product, how to solve this error? } String getName() { return storeName; } ArrayList&lt;Product&gt; getProductList() { return pList; } }</pre></div></object> - Java Method add ArrayList is undefined for the type <OBJECT>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中使用自定义类型ArrayList包含? - How To Use Contains on Custom Type ArrayList in java? Java检查ArrayList是否 <Cars> 包含对象 - Java check if ArrayList<Cars> contains object Java 将 object 类型的 ArrayList 转换为另一个 object 类型的 ArrayList - Java transform ArrayList of type object to another ArrayList of type object Java ArrayList:当 arraylist 包含给定对象时,contains() 方法返回 false - Java ArrayList: contains() method returns false when arraylist contains the given object 如何识别哪种类型的服装数组列表包含一个对象? - How to identify what type of costum arraylist contains an Object? 包含 ArrayList 的对象 - Object that contains ArrayList Java-访问ArrayList中对象的原始类型 - Java - Access original type of object in ArrayList Java:ArrayList返回Object而不是所需的类型 - Java: ArrayList returns Object instead of desired type Java 方法添加 ArrayList 未定义类型<object><div id="text_translate"><p>我正在为我的作业构建一个 java 程序,我必须将产品添加到特定商店。 尝试从 Store class 添加到 ArrayList 时遇到问题。</p><p> 我有 class 产品如下:</p><pre> class Product { private String pName; private int pPrice; private int pQty; public Product (String pName, int pPrice, int pQty) { this.pName = pName; this.pPrice = pPrice; this.pQty = pQty; } }</pre><p> class 存储如下:</p><pre> class Store { private String storeName; ArrayList&lt;Product&gt; pList =new ArrayList&lt;&gt;(); public Store() { String name = storeName; pList = new ArrayList&lt;Product&gt;(); } public Store(String newStoreName,ArrayList&lt;Product&gt; newPList) { this.storeName = newStoreName; this.pList = newPList; } void setName(String storeName) { this.storeName = storeName; } void setProduct(Product pList) { pList.add(this.pList);//This return method add undefined for type Product, how to solve this error? } String getName() { return storeName; } ArrayList&lt;Product&gt; getProductList() { return pList; } }</pre></div></object> - Java Method add ArrayList is undefined for the type <OBJECT> 如何检查 object 是否是 java 中 arraylist 中的枚举类型? - How to check if a object is a type of enum in arraylist in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM