zl程序教程

您现在的位置是:首页 >  后端

当前栏目

java 判断对象是否为空

JAVA对象 判断 是否 为空
2023-09-14 08:59:46 时间
     * 判断对象或对象数组中每一个对象是否为空: 对象为null,字符序列长度为0,集合类、Map为empty       *        * @param obj       * @return       */       public static boolean isNullOrEmpty(Object obj) {           if (obj == null)               return true;           if (obj instanceof CharSequence)               return ((CharSequence) obj).length() == 0;           if (obj instanceof Collection)               return ((Collection) obj).isEmpty();           if (obj instanceof Map)               return ((Map) obj).isEmpty();           if (obj instanceof Object[]) {               Object[] object = (Object[]) obj;               if (object.length == 0) {                   return true;               }               boolean empty = true;               for (int i = 0; i   object.length; i++) {                   if (!isNullOrEmpty(object[i])) {                       empty = false;                       break;                   }               }               return empty;           }           return false;       }  

读取excel文件,转化为一个二维数组:Object[][] arrays

但是excel中有空行,所以需要过滤Object[][] arrays中的空的一维数组:


    public static Object[][] filterEmpty(Object[][] arrays) {           int sumNotNull = 0;           /***           * 统计非空元素的总个数           */           for (int i = 0; i   arrays.length; i++) {               Object object = arrays[i];               if (!ValueWidget.isNullOrEmpty(object)                         !SystemUtil.isNullOrEmpty((Object[]) object)) {// 判断元素是否为空                   sumNotNull = sumNotNull + 1;               }           }           Object[][] filtedObjs = new Object[sumNotNull][];           int index = 0;           for (int i = 0; i   arrays.length; i++) {               Object[] object_tmp = arrays[i];               if (!ValueWidget.isNullOrEmpty(object_tmp)                         !SystemUtil.isNullOrEmpty((Object[]) object_tmp)) {// 判断元素是否为空                   filtedObjs[index++] = object_tmp;               }           }           return filtedObjs;       }  
     * @param obj       * @param isExcludeZero  :true:数值类型的值为0,则当做为空;----false:数值类型的值为0,则不为空       *        * @return       * @throws SecurityException       * @throws IllegalArgumentException       * @throws NoSuchFieldException       * @throws IllegalAccessException       */       public static boolean isNullObject(Object obj, boolean isExcludeZero)               throws SecurityException, IllegalArgumentException,               NoSuchFieldException, IllegalAccessException {           if(ValueWidget.isNullOrEmpty(obj)){//对象本身就为null               return true;           }           List Field  fieldList = ReflectHWUtils.getAllFieldList(obj.getClass());           boolean isNull = true;           for (int i = 0; i   fieldList.size(); i++) {               Field f = fieldList.get(i);               Object propertyValue = null;               try {                   propertyValue = getObjectValue(obj, f);               } catch (NoSuchFieldException e) {                   e.printStackTrace();               }               if (!ValueWidget.isNullOrEmpty(propertyValue)) {// 字段不为空                   if (propertyValue instanceof Integer) {// 是数字                       if (!((Integer) propertyValue == 0   isExcludeZero)) {                           isNull = false;                           break;                       }                   } else if (propertyValue instanceof Double) {// 是数字                       if (!((Double) propertyValue == 0   isExcludeZero)) {                           isNull = false;                           break;                       }                   }else if (propertyValue instanceof Float) {// 是数字                       if (!((Float) propertyValue == 0   isExcludeZero)) {                           isNull = false;                           break;                       }                   }else if (propertyValue instanceof Short) {// 是数字                       if (!((Short) propertyValue == 0   isExcludeZero)) {                           isNull = false;                           break;                       }                   }else {                       isNull = false;                       break;                   }               }           }           return isNull;       }  
    public void test_isNullObject() throws SecurityException,               IllegalArgumentException, NoSuchFieldException,               IllegalAccessException {           Person2 p = new Person2();           Assert.assertEquals(true, ReflectHWUtils.isNullObject(p, true));           Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, false));           p.setAddress("beijing");           Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, true));           Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, false));           p.setAddress(null);           p.setId(0);           Assert.assertEquals(true, ReflectHWUtils.isNullObject(p, true));           Assert.assertEquals(false, ReflectHWUtils.isNullObject(p, false));       }  
Java的对象克隆 Object 类提供的 clone() 方法是 “浅拷贝”,并没有克隆对象中引用的其他对象,原对象和克隆的对象仍然会共享一些信息。深拷贝指的是:在对象中存在其他对象的引用的情况下,会同时克隆对象中引用的其他对象,原对象和克隆的对象互不影响。
Java对象的序列化和反序列化 Java 对象的序列化和反序列化是一种将对象转换成字节流并存储在硬盘或网络中,以及从字节流中重新加载对象的操作。Java 的序列化和反序列化提供了一种方便的方式,使得可以将对象在不同的应用程序之间进行交互。
黄威的世界 我是一个热衷IT技术的人,希望自己不断地设计开发出对别人非常有用的软件。有近7年的java开发经验(包括2年Android开发经验)和一年左右的linux使用经验。擅长Java Web后台开发 ,喜欢研究新的各种实用技术