Can a single Java variable accept an array of either primitives or objects?

前端 未结 6 618
走了就别回头了
走了就别回头了 2021-01-03 12:30

For example, for a single method that reads the elements of an array, how can the programmer allow either an array of objects or an array of primitives to be passed as the p

6条回答
  •  甜味超标
    2021-01-03 13:36

    You can pass an array of either type as an Object.

    From there, you have to use reflection. In particular isArray(), getComponentType(), and isPrimitive() will tell the method what it has been passed. It's some of the unfortunate untidiness introduced by primitive data types.

提交回复
热议问题