I was wondering if it is possible to change to change the length of a class's integer array using the Java Reflection API. If so, how?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
An array is a fixed length data structure, so there is no way that it's length will be modified. Nevertheless, one can create a new array with a new fixed length in such way it can accommodate new members using
It is like you have an array of type T with the size of 2,
and it is length is fixed with 2. So it can not store any more than 2 elements. But by creating new array with a new fixed length, say 5,
So it can accommodate 5 elements now. Now copy the contents of the t1 to t2 using
in this case of the example,
Now in the new array, you have position
is available for you to use.
Nope; an array is created with a fixed length.
What you can do is get close by modifying the value of the field with a copy in larger array (using
Arrays.copyOf
), so long as you know modifying like this won't cause any inconsistency.I guess java will not allow you to change array length but yes you can set value at index using reflection.
I don't think it's possible to change array length even with Reflection.
This is a reference from java tutorial.
http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html