【问题标题】:Looping through set循环遍历集合
【发布时间】:2016-05-01 18:47:24
【问题描述】:

我创建了两个类。第一个(称为 Class1)有一个私有属性:价格。第二个(称为Class2)需要有一组Class1的对象:

我的代码:

private HashSet set = new HashSet<Class1>();

目标是在 Class2 中创建一个方法,该方法接受一个 int 作为参数并遍历对象以检查,直到找到一个价格等于给定参数编号的对象。它需要返回对象。例如,我想找到一个价格为 500 的对象,所以我调用函数 check(500),它返回的正是那个价格为 500 的对象。我该怎么做?


我的代码:

  public Class1 check(int p)
    { 
     Class1 c = new Class1(p);
     Iterator it = set.iterator();
     while(it.hasNext())
     {
     // HERE IS THE HELP NEEDED. Using an array it  
     // would be sth like if(element[i].price == p)
     // but I need to use set
      if()
      {}
    it.next();


    }

【问题讨论】:

标签: java loops set


【解决方案1】:

您可以这样做 - 但是您需要访问 price 才能进行比较。如果价格本身是私有的,那么应该有一个吸气剂,我在我的解决方案中假设。

public Class1 check(int p) {
    for (Class1 c : set) {
        if (c.getPrice() == p) return c;
    }
    return null; // none found
}

【讨论】:

    【解决方案2】:

    在你的类Class1中,定义一个getter方法

    int getPrice() { return price; } 
    

    在你的课堂上使用它Class2

    Class1 curObj = it.next(); 
    if (curObj.getPrice() == p) {
        your logic
    }
    

    【讨论】:

      【解决方案3】:

      这样的事情应该可以工作:

      public Class1 check(int p) {
          Iterator<Class1> it = set.iterator();
          while (it.hasNext()) {
              Class1 next = it.next();
              if (next.getPrice() == p) {
                  return next;
              }
          }
          return null;
      }
      

      我假设你的 Class1 类有一个返回价格的 getPrice 方法,如果无法访问该字段,当然不可能将我们需要找到的价格与对象的价格进行比较。

      【讨论】:

        【解决方案4】:

        您的泛型类型声明不正确,因此您有一个 raw type;你应该更喜欢Set 接口。类似的东西

        private Set<Class1> set = new HashSet<>();
        

        您可以使用增强的for-each loop 来迭代您的set 项目并找到匹配的price 类似

        public Class1 check(int p) {
            for (Class1 item : set) {
                if (item.getPrice() == p) {
                    return item;
                }
            }
            return null;
        }
        

        【讨论】:

          【解决方案5】:

          只要你有私有变量,就通过 getter 函数公开它们。 每当您使用 Spring、Hibernate 等框架编写代码时,Setter 和 getter 将始终为您提供帮助。

          public class class2 {
          
              private HashSet set = new HashSet<Class1>();
          
              public Class1 check(int price) {
                  Iterator<Class1> iterator = set.iterator();
                  while(iterator.hasNext()) {
                      Class1 next = iterator.next();
                      if(next.getPrice() == price)
                          return next;
                  }
                  return null;
              }
          }
          
          class Class1 {
              private int price;
          
              public int getPrice() {
                  return price;
              }
          
              public void setPrice(int price) {
                  this.price = price;
              }
          }
          

          【讨论】:

            【解决方案6】:

            您可以对集合进行迭代,并在每次迭代中将对象与价格参数进行比较,如果找到则返回对象,如果没有则返回 null

            示例

            public Class1 check(int p)
                { 
                 
                 for(Class c : set){
                     if(c.getPrice ()==p){
                          return c;
                     }
                 }
                 return null;
                }
            

            【讨论】:

              【解决方案7】:

              在class1中添加一个price属性的getter,那么check方法可以如下:

              public Class1 check(int p) 
              {
                for (Class1 c1 : set) 
                {
                  if (c1.getPrice() == p) 
                  return c1;
                } 
                return null
              }
              

              【讨论】:

                猜你喜欢
                • 2018-06-16
                • 2016-07-16
                • 2014-08-13
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2017-06-28
                • 2021-09-12
                相关资源
                最近更新 更多