I wanted the following function to be generic for all data types. However, this does not work with the primitive data types. Why is that? Can anyone give a solution to this? I would greatly appreciate it.
private int getIndexOfElement(Object x, Object[] xArray){
for(int i=0; i<xArray.length;i++){
if(xArray[i]==x){
return i;
}
}
return -1;
}
java.lang.reflect.Array, but almost certainly not recommended. Reflection is clunky and error-prone so you'd think carefully about whether it's actually a good decision to use it. Useful to know about, though.Arrays.stream( data ).boxed().toArray( Integer[]::new )to convert a primitives array to an objects array.