java 怎样 改变 数组元素的值

2023-05-24,,

简介 (Introduction):

背景 需要解析Object数组中的数据,将数据(mintime)进行修改,改为(maxtime),修改后,生成新的对象

结构图

核心 对于Object数组的概念理解,对于数组的解析理解,数组赋值的理解 详见:https://www.cnblogs.com/liuyangfirst/p/12364850.html

快速上手(Getting Started)

测试数据
1、创建测试对象

 class FunctionType {

     private String functionType;

     private Object[] objects;

     private boolean isDistinct;

     public String getFunctionType() {
return functionType;
} public void setFunctionType(String functionType) {
this.functionType = functionType;
} public Object[] getObjects() {
return objects;
} public void setObjects(Object[] objects) {
this.objects = objects;
} public boolean isDistinct() {
return isDistinct;
} public void setDistinct(boolean distinct) {
isDistinct = distinct;
} @Override
protected Object clone() { FunctionType functionTypeDemo = new FunctionType(); if (objects != null) {
functionTypeDemo.setObjects(objects.clone());
} functionTypeDemo.setFunctionType(functionType); functionTypeDemo.setDistinct(isDistinct); return functionTypeDemo;
}
}
2、创建测试数据

         FunctionType functionType = new FunctionType();

         functionType.setDistinct(false);
functionType.setFunctionType("max");
Object[] objects2 = new Object[1];
objects2[0] = "mintime";
functionType.setObjects(objects2); FunctionType functionType2 = new FunctionType(); functionType2.setFunctionType("from_unixtime");
functionType2.setDistinct(false);
Object[] objects3 = new Object[2];
objects3[0] = functionType;
objects3[1] = "yyyy-MM-dd HH24:mi:ss"; functionType2.setObjects(objects3);
3、创建修改方法

  private static void changeObjectParam(FunctionType functionType2, String string) {
Object[] objects = functionType2.getObjects(); // 替换
Object[] replace = new Object[1];
replace[0] = new String(string); for (int i = 0; i < objects.length; i++) {
if (objects[0] instanceof FunctionType) {
FunctionType o = (FunctionType) objects[0];
if("max".equalsIgnoreCase(o.getFunctionType())){
if("mintime".equalsIgnoreCase(o.getObjects()[0].toString())){
o.setObjects(replace);
}
} break;
}
} System.out.println(new Gson().toJson(functionType2));
}
4、测试

 public static void main(String[] args) {

         FunctionType functionType = new FunctionType();

         functionType.setDistinct(false);
functionType.setFunctionType("max");
Object[] objects2 = new Object[1];
objects2[0] = "mintime";
functionType.setObjects(objects2); FunctionType functionType2 = new FunctionType(); functionType2.setFunctionType("from_unixtime");
functionType2.setDistinct(false);
Object[] objects3 = new Object[2];
objects3[0] = functionType;
objects3[1] = "yyyy-MM-dd HH24:mi:ss"; functionType2.setObjects(objects3); String string ="replace"; // 修改mintime为maxtime
// 修改mintime为maxtime
changeObjectParam(functionType2, string); }

环境 JDK1.8

配置 IDEA编辑

存在问题 暂无

进阶篇 (Advanced):
拓展object[]数据操作方法
1、根据传入的key,获取object中存的值
(1)方法

 /**
* 获取object中存的值
*
* @param obj 传入的Object
* @param key 传入的字段名称
* @return 获取object中存的值
*/
public static String getValueByKey(Object obj, String key) { // 得到类对象
Class<?> objClass = obj.getClass();
// 得到类中的所有属性集合
Field[] fs = objClass.getDeclaredFields(); for (Field f : fs) { // 设置些属性是可以访问的
f.setAccessible(true); try { if (f.getName().endsWith(key)) {
return f.get(obj).toString();
} } catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
} return "";
}
(2)测试

 public static void main(String[] args) {

         Object[] objects = new Object[5];
UserVO userVO = new UserVO("zhansan", "888");
objects[0] = userVO;
objects[1] = new Date();
// name 是UserVO对象的字段属性名
String name = getValueByKey(objects[0], "name");
System.out.println("value: " + name);
}
2、根据传入的值,获取object中存的key
(1)方法

 /**
* 获取object中存的关键字
*
* @param obj 传入的Object
* @return 获取object中存的关键字
*/
public static String getKey(Object obj) { // 得到类对象
Class<?> objClass = obj.getClass();
// 得到类中的所有属性集合
Field[] fs = objClass.getDeclaredFields(); for (Field f : fs) { // 设置些属性是可以访问的
f.setAccessible(true); try { return f.getName(); } catch (IllegalArgumentException e) {
e.printStackTrace();
}
} return "";
}
(2)测试

 public static void main(String[] args) {

         Object[] objects = new Object[5];
UserVO userVO = new UserVO("zhansan", "888");
objects[0] = userVO;
objects[1] = new Date(); String key = getKey(objects[0]);
System.out.println("key: " + key);
}
3、根据传入的key,修改object[]中的对象的值
(1)方法

 /**
* 修改object中参数的值
*
* @param obj 传入的Object
* @param key 传入的字段名称
* @param newValue 改变的值
*/
public static void changeObjectValueByKey(Object obj, String key, String newValue) { // 得到类对象
Class<?> objClass = obj.getClass();
// 得到类中的所有属性集合
Field[] fs = objClass.getDeclaredFields(); for (Field f : fs) { // 设置些属性是可以访问的
f.setAccessible(true);
try { if (f.getName().endsWith(key)) {
if (newValue.equalsIgnoreCase(f.get(obj).toString())) {
f.set(obj, key);
}
} } catch (IllegalArgumentException | IllegalAccessException e) {
e.printStackTrace();
}
}
}
(2)测试

 public static void main(String[] args) {

         Object[] objects = new Object[5];
UserVO userVO = new UserVO("zhansan", "888");
objects[0] = userVO;
objects[1] = new Date(); changeObjectValueByKey(objects[0], "name", "lisi");
System.out.println("user: " + new Gson().toJson(objects));
}
4、根据传入的索引,修改object[]中的对象的值
(1)方法

 public static void changeObjectValue(Object[] obj, int index, String newValue) {

         for (int i = 0; i < obj.length; i++) {

             if (i == index) {

                 if (obj[i] instanceof Object[]) {
Object[] objects = (Object[]) obj[i];
objects[0] = newValue;
}
}
}
}
(2)测试

 public static void main(String[] args) {

         Object[] objects = new Object[5];
UserVO userVO = new UserVO("zhansan", "888");
objects[0] = userVO;
objects[1] = new Date(); changeObjectValue(objects, 0, "lisi");
System.out.println("user: " + new Gson().toJson(objects));
}
5、修改object中的值
(1)方法

 public static String getChildValueFromList(Object[] obj, int index) {

         for (int i = 0; i < obj.length; i++) {

             if (i == index) {

                 if (obj[i] instanceof Object[]) {
Object[] objects = (Object[]) obj[i];
return objects[0].toString();
}
}
} return "";
}
(2)测试

 public static void main(String[] args) {

         Object[] objects = new Object[5];
UserVO userVO = new UserVO("zhansan", "888");
objects[0] = userVO;
objects[1] = new Date(); String childValue = getChildValueFromList(objects, 2);
System.out.println("childValue: " + childValue);
}

java 怎样 改变 数组元素的值的相关教程结束。

《java 怎样 改变 数组元素的值.doc》

下载本文的Word格式文档,以方便收藏与打印。