Refactoring methods that use the same code but different types

后端 未结 5 1848
情深已故
情深已故 2020-12-31 14:22

I have several methods that do the same thing yet, when interfacing with the MySQL database, save or load a different type of parameter. Currently, I have a different method

5条回答
  •  南笙
    南笙 (楼主)
    2020-12-31 15:15

    I would just use long[] instead of int[]. The memory difference is very small compared with the cost of using JDBC.

    If you need to handle String you can use an object type.

    public static void saveArray(Connection con, int playerID, String tableName, 
        String fieldName, Object[] array, Object[] originalArray) {
    

    If you want one method for long[] and Object[] you can use the Array.getLength() and Array.get() method to access all array types generically. This could add more complexity than it saves.

提交回复
热议问题