Is it possible to reuse the same function for different datatypes? So for example I have a function that converts an Integer ArrayList into an Integer array
public static int[] arrayListToIntArray(ArrayList<Integer> list) {
int[] out = new int[list.size()];
int count = 0;
for (int x : list) {
out[count] = x;
count++;
}
return out;
}
However if I want to do that with a byte ArrayList I have to do this
public static byte[] arrayListToByteArray(ArrayList<Byte> list) {
byte[] out = new byte[list.size()];
int count = 0;
for (byte x : list) {
out[count] = x;
count++;
}
return out;
}
So I am wondering if there is a better way than just repeating the same code with different data types and basically having an entire class of the same code? Or could I do something so it can be used for all data types?