Object o = new Long[0] System.out.println( o.getClass().isArray() ) System.out.println( o.getClass().getName() ) Class ofArray = ???
Running the first 3 lines emits;
true [Ljava.lang.Long;
How do I get ??? to be type long? I could parse the string and do a Class.forname(), but thats grotty. What's the easy way?
Well, you can get the element type of the array: Type type = array. GetType(). GetElementType();
Given task is to implement a class in Java which behaves just like the Dynamic array using ArrayList. ArrayList is same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.
Introduction to Dynamic Array in JavaThe dynamic array is such a type of an array with a huge improvement for automatic resizing. The only limitation of arrays is that it is a fixed size. This translates into a meaning that you can only specify the number of elements that your array can hold ahead of time.
Just write
Class ofArray = o.getClass().getComponentType();
From the JavaDoc:
public Class<?> getComponentType()
Returns the
Class
representing the component type of an array. If this class does not represent an array class this method returnsnull
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With