从对象数组到对象不能转换为java.lang.Object

时间:2019-03-15 15:17:44

标签: java object casting

在执行"Object[] cols = (Object[]) o;"代码时,for循环中出现强制转换异常。我不应该更改for循环的实现,因为这段代码共享在prod Environment中。

public class Cast {
   public static void main(String args[])
   {
          List<Object> resultList = new ArrayList<Object>();

          AppGrpCBSModel appGrpCBSModel = new AppGrpCBSModel();
          RCBSAppGroupModel rcbsAppGroupModel = new RCBSAppGroupModel();
          rcbsAppGroupModel.setSortCode("012345");
          appGrpCBSModel.setHeritage(HeritageNm.HBOS.value());
          resultList.add(rcbsAppGroupModel);
          resultList.add(appGrpCBSModel);


          if (!resultList.isEmpty() && resultList != null) {

                 for (Object o : resultList) {
                       Object[] cols = (Object[]) o;
                       appGrpCBSModel = (AppGrpCBSModel) cols[0];
                       rcbsAppGroupModel = (RCBSAppGroupModel) cols[1];

   }
          }
   }
}

1 个答案:

答案 0 :(得分:0)

您正在遍历一个 MirrorSystem mirrors = currentMirrorSystem(); ClassMirror classMirror = mirrors.findLibrary(Symbol.empty).declarations[new Symbol('Opacity')]; print(classMirror); var arguments = {'a': 'a', 'b': 'b', 'c': 'c'}.map((key, value) { return MapEntry(Symbol(key), value); }); var op = classMirror.newInstance(Symbol.empty, [], arguments); Opacity opacity = op.reflectee; print("opacity.a: ${opacity.a}"); print("opacity.b: ${opacity.b}"); print("opacity.c: ${opacity.c}"); ,但是您试图将列表的每个成员强制转换为List<Object>而不是Object[]。您需要将列表更改为Object或强制转换为List<Object[]>而不是Object