java 怎樣 改變 陣列元素的值
阿新 • • 發佈:2020-07-08
簡介 (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);
}