简体   繁体   English

如何使我的循环更通用?

[英]How to make my loops more generic?

Below is my code. 下面是我的代码。 As you will see there is 2 almost identical blocks of code. 如您所见,这里有2个几乎相同的代码块。 The only thing that is different is the type of the object. 唯一不同的是对象的类型。 I am looking for a way to make it more generic. 我正在寻找一种使其更通用的方法。 To have one block of code and just set the type (Owner, Car) as a param in my method. 要获得一个代码块,只需在我的方法中将类型(车主,汽车)设置为参数。 Any hint? 有什么提示吗? I've tried using reflection but I never was able to make it dynamic cause I had to loop over the collection 我尝试使用反射,但由于无法遍历集合而无法使其动态化

if(queryResult.get(0) instanceof Owner) {
        classFields = Owner.class.getDeclaredFields();
        data = new Object[queryResult.size()][classFields.length];
        for(Owner owner : (List<Owner>)(Object)queryResult) {
            int rCounter = 0;
            for(Field field : owner.getClass().getDeclaredFields()) {
                field.setAccessible(true);
                try {
                    data[lCounter][rCounter] = field.get(owner);
                } catch (IllegalArgumentException e1) {
                    e1.printStackTrace();
                } catch (IllegalAccessException e1) {
                    e1.printStackTrace();
                }

                rCounter++;
            }
            lCounter++;
        }
    } else if(queryResult.get(0) instanceof Car) {
        classFields = Car.class.getDeclaredFields();
        data = new Object[queryResult.size()][classFields.length];
        for(Car car : (List<Car>)(Object)queryResult) {
            int rCounter = 0;
            for(Field field : car.getClass().getDeclaredFields()) {
                field.setAccessible(true);
                try {
                    data[lCounter][rCounter] = field.get(car);
                } catch (IllegalArgumentException e1) {
                    e1.printStackTrace();
                } catch (IllegalAccessException e1) {
                    e1.printStackTrace();
                }

                rCounter++;
            }
            lCounter++;
        }
    }

You should be able to collapse your entire code block down to: 您应该可以将整个代码块折叠为:

 Class clazz = queryResult.get(0).getClass();
 classFields = clazz.getDeclaredFields();
 data = new Object[queryResult.size()][classFields.length];
 for(Object result : queryResult) {
     int rCounter = 0;
     for(Field field : clazzFields) {
         field.setAccessible(true);
          try {
              data[lCounter][rCounter] = field.get(result);
          } catch (IllegalArgumentException e1) {
               e1.printStackTrace();
          } catch (IllegalAccessException e1) {
               e1.printStackTrace();
          }
          rCounter++;
      }
      lCounter++;
}

Warning: I didn't compile and run this to check 警告:我没有编译并运行此程序进行检查

You don't need this code repetition. 您不需要此代码重复。 Just use car and owner 只是carowner
as instances of Object . 作为Object实例。 You don't use the types Car and Owner 您不使用“ Car和“ Owner ”类型
in any different/meaningful way. 以任何不同/有意义的方式。 So your code will continue to work fine 因此您的代码将继续正常运行
because both cases are processed the same way. 因为两种情况都以相同的方式处理。 Seems the only thing 似乎唯一
you need to change is this: 您需要更改的是:

Car.class.getDeclaredFields(); and
Owner.class.getDeclaredFields();

to this: 对此:

queryResult.get(0).getClass().getDeclaredFields();

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM