Is there a Java equivalent to NumPy

Java blog for programmers

The static methods Arrays.equals (...) compare whether two arrays have the same content; the overloaded method is defined for all important types. If two arrays actually have the same content, the return of the method is true, otherwise false. Of course, both arrays must already have the same number of elements, otherwise the test is over immediately and the result is false. In the case of object arrays, Arrays.equals (...) does not use the identity check via ==, but rather the equality via equals (...).


Compare three arrays:

int [] array1 = {1, 2, 3, 4}; int [] array2 = {1, 2, 3, 4}; int [] array3 = {9, 9, 2, 3, 9}; System.out.println ( Arrays.equals (array1, array2) ); // true System.out.println ( Arrays.equals (array2, 1, 3, array3, 2, 4) ); // true

A comparison of sub-arrays was only added in Java 9.

With underreferenced arrays, Arrays.equals (...) regards the inner array as an object reference and also compares it with equals (...) - which means, however, that inner arrays that are not identical but referenced with the same elements are regarded as unequal. The static method deepEquals (...) also includes subreferenced arrays in the comparison.


Difference between equals (...) and deepEquals (...):

int [] [] a1 = {{0, 1}, {1, 0}}; int [] [] a2 = {{0, 1}, {1, 0}}; System.out.println (Arrays.equals (a1, a2)); // false System.out.println (Arrays.deepEquals (a1, a2)); // true System.out.println (a1 [0]); // for example [I @ 10b62c9 System.out.println (a2 [0]); // for example [I @ 82ba41

The last two console outputs show that the methods work differently: The arrays subreferenced by a1 and a2 contain the same elements, but are two different objects, i.e. not identical.


deepEquals (...) also compares one-dimensional arrays:

Object [] b1 = {"1", "2", "3"}; Object [] b2 = {"1", "2", "3"}; System.out.println (Arrays.deepEquals (b1, b2)); // true

class java.util.Arrays

  • staticbooleanequals (XXX [] a, XXX [] a2)
    Compares two arrays of the same type and returns true if the arrays are of the same size and elements are the same in pairs. XXX stands for boolean, byte, char, int, short, long, double, float.
  • staticbooleanequals (XXX[] a, int aFromIndex, int aToIndex, XXX[] b, int bFromIndex, int bToIndex)
    Compares two arrays, but stays in the selected sections. New methods in Java 9.
  • staticbooleanequals (Object [] a, Object [] a2)
    Compares two arrays of object references. An object array can contain zero; then for equality e1 == zero? e2 == null: equals (e2).
  • staticbooleandeepEquals (Object [] a1, Object [] a2)
    Returns true if the two arrays as well as all sub-arrays - recursively in the case of sub-object arrays - are the same.

similar posts

Posted in General