I just notice a weird behavior, look at the example:
public static void main(String[] args) {
aaa(1,2);
}
public static void aaa(int... a){
bbb(a);
ccc(a);
}
public static void bbb(int... a){
}
public static void ccc(Object... a){
}
On bbb a is an array {1,2} (a = {1,2})
On ccc a is an array of array's which the first array value is {1,2} (a = { {1,2} })
There are places on my code that I must define it as Object. But of course the second behavior is not desired.. I want the same behavior for both definitions (both Object... and int... will contain {1,2}). How do I do it?
The preferred value is an array of values and not and array of arrays. For example bbb parameter is ok, and the desired parameter in ccc is {1,2} from type Object.
If will try to do something like this is order to get the inner array I will fail:
public static void ccc(Object... a){
ArrayList<Object> list = new ArrayList<>();
if(a.getClass().isArray()){
for(Object in_obj: (Object[])obj){
list.add(in_obj);
}
}
// rest of the code..
}
Error: java.lang.ClassCastException: I cannot be cast to java.lang.Object[] because the array is primitive.. Is there a solution for that? I dont use only with int, but with a lot of primitive(and not primitive)..