Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to know if type is array or not  RSS feed

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im getting from method the parameters types

Class[] types = currentMethod.getParameterTypes();

now i like to know if types[i] is array or not
if i do : types[j].getName() im getting only the type name but no indication if its array or not

how can i do that ?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"types[i]" is an instance of java.lang.Class. Did you look carefully at the Javadocs for this class? For example, are there any methods with "array" in their names that look promising?
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sorry i meant how to know if its 2 dimensional array
simple case to know if its array i will use isArray
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java "2D arrays" are actually arrays of arrays. The Class.getComponentType() method tells you the type of the data an array can hold; for a "2D array", this will be another array type. So something like



Of course, this also returns true for "3D", "4D", etc, arrays.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!