Java Method with Enforced Array Size Parameters?

前端 未结 3 1088
闹比i
闹比i 2020-12-10 16:23

I would like to create an initialisation method for a Java class that accepts 3 parameters:

Employee[] method( String[] employeeNames, Integer[] employeeAges         


        
3条回答
  •  猫巷女王i
    2020-12-10 16:57

    Since the arrays being passed in aren't generated until runtime, it is not possible to prevent the method call from completing depending upon the characteristics of the array being passed in as a compile-time check.

    As Jon Skeet has mentioned, the only way to indicate a problem is to throw an IllegalArgumentException or the like at runtime to stop the processing when the method is called with the wrong parameters.

    In any case, the documentation should clearly note the expectations and the "contract" for using the method -- passing in of three arrays which have the same lengths. It would probably be a good idea to note this in the Javadocs for the method.

提交回复
热议问题