java 怎样 改变 数组元素的值
-
简介 (Introduction):
-
背景 需要解析Object数组中的数据,将数据(mintime)进行修改,改为(maxtime),修改后,生成新的对象
-
结构图
-
核心 对于Object数组的概念理解,对于数组的解析理解,数组赋值的理解 详见:https://www.cnblogs.com/liuyangfirst/p/12364850.html
-
快速上手(Getting Started)
-
测试数据
1、创建测试对象
1 class FunctionType { 2 3 4 private String functionType; 5 6 private Object[] objects; 7 8 9 private boolean isDistinct; 10 11 12 public String getFunctionType() { 13 return functionType; 14 } 15 16 public void setFunctionType(String functionType) { 17 this.functionType = functionType; 18 } 19 20 public Object[] getObjects() { 21 return objects; 22 } 23 24 public void setObjects(Object[] objects) { 25 this.objects = objects; 26 } 27 28 public boolean isDistinct() { 29 return isDistinct; 30 } 31 32 public void setDistinct(boolean distinct) { 33 isDistinct = distinct; 34 } 35 36 @Override 37 protected Object clone() { 38 39 FunctionType functionTypeDemo = new FunctionType(); 40 41 if (objects != null) { 42 functionTypeDemo.setObjects(objects.clone()); 43 } 44 45 functionTypeDemo.setFunctionType(functionType); 46 47 functionTypeDemo.setDistinct(isDistinct); 48 49 return functionTypeDemo; 50 } 51 }
2、创建测试数据
1 FunctionType functionType = new FunctionType(); 2 3 functionType.setDistinct(false); 4 functionType.setFunctionType("max"); 5 Object[] objects2 = new Object[1]; 6 objects2[0] = "mintime"; 7 functionType.setObjects(objects2); 8 9 FunctionType functionType2 = new FunctionType(); 10 11 functionType2.setFunctionType("from_unixtime"); 12 functionType2.setDistinct(false); 13 Object[] objects3 = new Object[2]; 14 objects3[0] = functionType; 15 objects3[1] = "yyyy-MM-dd HH24:mi:ss"; 16 17 functionType2.setObjects(objects3);
3、创建修改方法
1 private static void changeObjectParam(FunctionType functionType2, String string) { 2 Object[] objects = functionType2.getObjects(); 3 4 5 // 替换 6 Object[] replace = new Object[1]; 7 replace[0] = new String(string); 8 9 for (int i = 0; i < objects.length; i++) { 10 if (objects[0] instanceof FunctionType) { 11 FunctionType o = (FunctionType) objects[0]; 12 if("max".equalsIgnoreCase(o.getFunctionType())){ 13 if("mintime".equalsIgnoreCase(o.getObjects()[0].toString())){ 14 o.setObjects(replace); 15 } 16 } 17 18 19 break; 20 } 21 } 22 23 24 System.out.println(new Gson().toJson(functionType2)); 25 }
4、测试
1 public static void main(String[] args) { 2 3 4 FunctionType functionType = new FunctionType(); 5 6 functionType.setDistinct(false); 7 functionType.setFunctionType("max"); 8 Object[] objects2 = new Object[1]; 9 objects2[0] = "mintime"; 10 functionType.setObjects(objects2); 11 12 FunctionType functionType2 = new FunctionType(); 13 14 functionType2.setFunctionType("from_unixtime"); 15 functionType2.setDistinct(false); 16 Object[] objects3 = new Object[2]; 17 objects3[0] = functionType; 18 objects3[1] = "yyyy-MM-dd HH24:mi:ss"; 19 20 functionType2.setObjects(objects3); 21 22 String string ="replace"; 23 24 // 修改mintime为maxtime 25 // 修改mintime为maxtime 26 changeObjectParam(functionType2, string); 27 28 29 }
-
环境 JDK1.8
- 配置 IDEA编辑
-
存在问题 暂无
-
进阶篇 (Advanced):
拓展object[]数据操作方法
1、根据传入的key,获取object中存的值
(1)方法1 /** 2 * 获取object中存的值 3 * 4 * @param obj 传入的Object 5 * @param key 传入的字段名称 6 * @return 获取object中存的值 7 */ 8 public static String getValueByKey(Object obj, String key) { 9 10 // 得到类对象 11 Class<?> objClass = obj.getClass(); 12 // 得到类中的所有属性集合 13 Field[] fs = objClass.getDeclaredFields(); 14 15 for (Field f : fs) { 16 17 // 设置些属性是可以访问的 18 f.setAccessible(true); 19 20 try { 21 22 if (f.getName().endsWith(key)) { 23 return f.get(obj).toString(); 24 } 25 26 } catch (IllegalArgumentException | IllegalAccessException e) { 27 e.printStackTrace(); 28 } 29 } 30 31 return ""; 32 }
(2)测试
1 public static void main(String[] args) { 2 3 Object[] objects = new Object[5]; 4 UserVO userVO = new UserVO("zhansan", "888"); 5 objects[0] = userVO; 6 objects[1] = new Date(); 7 // name 是UserVO对象的字段属性名 8 String name = getValueByKey(objects[0], "name"); 9 System.out.println("value: " + name); 10 }
2、根据传入的值,获取object中存的key
(1)方法
1 /** 2 * 获取object中存的关键字 3 * 4 * @param obj 传入的Object 5 * @return 获取object中存的关键字 6 */ 7 public static String getKey(Object obj) { 8 9 // 得到类对象 10 Class<?> objClass = obj.getClass(); 11 // 得到类中的所有属性集合 12 Field[] fs = objClass.getDeclaredFields(); 13 14 for (Field f : fs) { 15 16 // 设置些属性是可以访问的 17 f.setAccessible(true); 18 19 try { 20 21 return f.getName(); 22 23 } catch (IllegalArgumentException e) { 24 e.printStackTrace(); 25 } 26 } 27 28 return ""; 29 }
(2)测试
1 public static void main(String[] args) { 2 3 Object[] objects = new Object[5]; 4 UserVO userVO = new UserVO("zhansan", "888"); 5 objects[0] = userVO; 6 objects[1] = new Date(); 7 8 String key = getKey(objects[0]); 9 System.out.println("key: " + key); 10 }
3、根据传入的key,修改object[]中的对象的值
(1)方法
1 /** 2 * 修改object中参数的值 3 * 4 * @param obj 传入的Object 5 * @param key 传入的字段名称 6 * @param newValue 改变的值 7 */ 8 public static void changeObjectValueByKey(Object obj, String key, String newValue) { 9 10 // 得到类对象 11 Class<?> objClass = obj.getClass(); 12 // 得到类中的所有属性集合 13 Field[] fs = objClass.getDeclaredFields(); 14 15 for (Field f : fs) { 16 17 // 设置些属性是可以访问的 18 f.setAccessible(true); 19 try { 20 21 if (f.getName().endsWith(key)) { 22 if (newValue.equalsIgnoreCase(f.get(obj).toString())) { 23 f.set(obj, key); 24 } 25 } 26 27 } catch (IllegalArgumentException | IllegalAccessException e) { 28 e.printStackTrace(); 29 } 30 } 31 }
(2)测试
1 public static void main(String[] args) { 2 3 Object[] objects = new Object[5]; 4 UserVO userVO = new UserVO("zhansan", "888"); 5 objects[0] = userVO; 6 objects[1] = new Date(); 7 8 9 changeObjectValueByKey(objects[0], "name", "lisi"); 10 System.out.println("user: " + new Gson().toJson(objects)); 11 }
4、根据传入的索引,修改object[]中的对象的值
(1)方法
1 public static void changeObjectValue(Object[] obj, int index, String newValue) { 2 3 for (int i = 0; i < obj.length; i++) { 4 5 if (i == index) { 6 7 if (obj[i] instanceof Object[]) { 8 Object[] objects = (Object[]) obj[i]; 9 objects[0] = newValue; 10 } 11 } 12 } 13 }
(2)测试
1 public static void main(String[] args) { 2 3 Object[] objects = new Object[5]; 4 UserVO userVO = new UserVO("zhansan", "888"); 5 objects[0] = userVO; 6 objects[1] = new Date(); 7 8 9 changeObjectValue(objects, 0, "lisi"); 10 System.out.println("user: " + new Gson().toJson(objects)); 11 }
5、修改object中的值
(1)方法
1 public static String getChildValueFromList(Object[] obj, int index) { 2 3 for (int i = 0; i < obj.length; i++) { 4 5 if (i == index) { 6 7 if (obj[i] instanceof Object[]) { 8 Object[] objects = (Object[]) obj[i]; 9 return objects[0].toString(); 10 } 11 } 12 } 13 14 15 return ""; 16 }
(2)测试
1 public static void main(String[] args) { 2 3 Object[] objects = new Object[5]; 4 UserVO userVO = new UserVO("zhansan", "888"); 5 objects[0] = userVO; 6 objects[1] = new Date(); 7 8 String childValue = getChildValueFromList(objects, 2); 9 System.out.println("childValue: " + childValue); 10 }