java 怎樣 改變 數組元素的值

  • 簡介 (Introduction):

    •  背景 需要解析Object數組中的數據,將數據(mintime)進行修改,改為(maxtime),修改後,生成新的對象

    • 結構圖

    •  核心 對於Object數組的概念理解,對於數組的解析理解,數組賦值的理解 詳見://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 }

View Code

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);

View Code

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     }

View Code

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     }

View Code

  •  環境 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     }

      View Code

      (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 }

      View Code

      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     }

      View Code

      (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 }

      View Code

      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     }

      View Code

      (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 }

      View Code

      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     }

      View Code

      (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 }

      View Code

      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     }

      View Code

      (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 }

      View Code