isSameType
is a static method of the ArrayUtils
class that checks whether two arrays are of the same type, considering multi-dimensional arrays.
ArrayUtils
is defined in the Apache Commons Lang package. Apache Commons Lang can be added to the Maven project by adding the following dependency to the pom.xml
file.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
For other versions of the commons-lang package, refer to the Maven Repository.
ArrayUtils
You can import the ArrayUtils
class as follows:
import org.apache.commons.lang3.ArrayUtils;
public static boolean isSameType(final Object array1, final Object array2)
final Object array1
: first array
final Object array2
: second array
This method returns true
if the two arrays are of same type, and returns false
otherwise.
array1 = new int[]{1,2,3,4,5}
array2 = new int[]{1,2,3}
Applying the isSameType
function will result in true
, as the two arrays are of the same type.
array1 = new int[]{1,2,3,4,5}
array2 = new char[]{'a','b','v'}
Applying the isSameType
function will result in false
, as the two arrays are of different types.
import org.apache.commons.lang3.ArrayUtils;public class Main {public static void main(String[] args) {int[] array = new int[]{1,2,3,4,5,2,3,2,2,4};char[] array2 = new char[]{'a', 's', 'f'};boolean check = ArrayUtils.isSameType(array, array2);System.out.println("int array and boolean array2 are of same type - " + check);check = ArrayUtils.isSameType(array, array);System.out.println("int array and int array are of same type - " + check);}}
int array and boolean array2 are of same type - false
int array and int array are of same type - true