Comparing arrays in Java can be tricky, as the ==
operator only checks for memory location equality. At COMPARE.EDU.VN, we provide comprehensive guides to help you navigate these complexities. This article will explore various methods for effective array comparison, ensuring you choose the right approach for your needs. Explore different array comparison techniques, data structure considerations, and algorithm efficiencies.
1. Understanding Array Comparison in Java
When dealing with arrays in Java, it’s crucial to understand that the ==
operator doesn’t compare the contents of the arrays. Instead, it checks if two array variables point to the same memory location. This means that if you have two arrays with the same elements but stored in different memory locations, ==
will return false
. To accurately compare arrays, you need to use methods that compare the actual elements within the arrays. Let’s explore how to compare Java arrays effectively, focusing on content comparison.
1.1. The Pitfalls of Using ==
for Array Comparison
The ==
operator in Java is used to check if two references point to the same object in memory. When applied to arrays, it doesn’t compare the elements within the arrays; rather, it checks if the two array variables refer to the same array object. This can lead to unexpected results if you’re trying to determine if two arrays have the same contents.
For example:
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
if (arr1 == arr2) {
System.out.println("Arrays are the same");
} else {
System.out.println("Arrays are not the same"); // This will be printed
}
In this case, even though arr1
and arr2
contain the same elements, the output will be “Arrays are not the same” because they are two distinct array objects in memory. This is a common mistake that many Java developers make when first working with arrays.
1.2. Why Content Comparison is Essential
Content comparison is crucial when you need to determine if two arrays have the same elements in the same order. This is particularly important in scenarios such as:
- Testing: Verifying that the output of a method matches the expected array.
- Data Validation: Ensuring that input data conforms to a specific array structure.
- Searching: Finding an array within a collection of arrays.
- Data Structures: Implementing data structures that rely on array equality.
To perform content comparison, you need to iterate through the arrays and compare each element individually. Java provides built-in methods to simplify this process, which we will explore in the following sections.
1.3. The Role of COMPARE.EDU.VN
in Array Comparisons
COMPARE.EDU.VN
is dedicated to providing detailed and objective comparisons of various programming techniques, including array comparisons in Java. We offer comprehensive guides that help you understand the nuances of different comparison methods and choose the most appropriate one for your specific needs. Our resources include:
- In-depth articles: Explaining the different methods for comparing arrays.
- Code examples: Illustrating how to use each method with clear, concise code.
- Performance analysis: Comparing the efficiency of different methods.
- Best practices: Recommending the best approach for different scenarios.
By leveraging the resources at compare.edu.vn
, you can ensure that you’re using the most effective and efficient methods for array comparison in your Java projects.
2. Using Arrays.equals()
for Shallow Comparison
The Arrays.equals()
method is a straightforward way to compare the contents of two arrays in Java. It performs a shallow comparison, meaning it compares the elements directly without recursively comparing nested arrays or objects. This method is suitable for comparing arrays of primitive types or arrays of objects where you only need to check if the references are equal.
2.1. How Arrays.equals()
Works
The Arrays.equals()
method iterates through the two arrays, comparing corresponding elements using the equals()
method of the element type. For primitive types, it directly compares the values. For objects, it calls the equals()
method of the objects to determine if they are equal. The method returns true
if all corresponding elements are equal and the arrays have the same length; otherwise, it returns false
.
The basic syntax for using Arrays.equals()
is:
boolean isEqual = Arrays.equals(array1, array2);
Here, array1
and array2
are the arrays you want to compare, and isEqual
is a boolean variable that will store the result of the comparison.
2.2. Example: Comparing Integer Arrays with Arrays.equals()
Let’s look at an example of how to use Arrays.equals()
to compare two integer arrays:
import java.util.Arrays;
public class ArrayComparison {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
int[] arr3 = {1, 2, 4};
System.out.println("arr1 equals arr2: " + Arrays.equals(arr1, arr2)); // Output: true
System.out.println("arr1 equals arr3: " + Arrays.equals(arr1, arr3)); // Output: false
}
}
In this example, Arrays.equals(arr1, arr2)
returns true
because the arrays have the same length and the same elements in the same order. Arrays.equals(arr1, arr3)
returns false
because the arrays have different elements.
2.3. Limitations of Arrays.equals()
for Deep Comparison
Arrays.equals()
performs a shallow comparison, which means it only compares the elements directly. If the arrays contain nested arrays or objects, it doesn’t recursively compare the contents of those nested structures. This can lead to incorrect results when comparing complex data structures.
For example:
import java.util.Arrays;
public class ArrayComparison {
public static void main(String[] args) {
Integer[] arr1 = {new Integer(1), new Integer(2), new Integer(3)};
Integer[] arr2 = {new Integer(1), new Integer(2), new Integer(3)};
System.out.println("arr1 equals arr2: " + Arrays.equals(arr1, arr2)); // Output: false
}
}
In this case, even though arr1
and arr2
contain Integer
objects with the same values, the output is false
because Arrays.equals()
only compares the references of the Integer
objects, not their actual values. To perform a deep comparison, you need to use Arrays.deepEquals()
, which we will discuss in the next section.
3. Deep Comparison with Arrays.deepEquals()
When dealing with arrays containing nested arrays or objects, Arrays.equals()
falls short because it only performs a shallow comparison. To accurately compare such arrays, you need to use Arrays.deepEquals()
. This method recursively compares the contents of the arrays, ensuring that nested structures are also compared element by element.
3.1. Understanding Arrays.deepEquals()
The Arrays.deepEquals()
method is designed to handle complex array structures by recursively traversing through nested arrays and objects. It compares elements using the following rules:
- If the elements are primitive types, it compares their values directly.
- If the elements are objects, it calls the
equals()
method of the objects. - If the elements are arrays, it recursively calls
Arrays.deepEquals()
on the nested arrays.
This ensures that the entire structure of the arrays is compared, not just the top-level elements. The syntax for using Arrays.deepEquals()
is similar to Arrays.equals()
:
boolean isEqual = Arrays.deepEquals(array1, array2);
3.2. Example: Comparing Nested Arrays with Arrays.deepEquals()
Let’s illustrate how Arrays.deepEquals()
can be used to compare nested arrays:
import java.util.Arrays;
public class DeepArrayComparison {
public static void main(String[] args) {
Integer[] arr1 = {new Integer(1), new Integer(2), new Integer(3)};
Integer[] arr2 = {new Integer(1), new Integer(2), new Integer(3)};
Object[] nestedArr1 = {arr1};
Object[] nestedArr2 = {arr2};
System.out.println("Arrays.equals(): " + Arrays.equals(nestedArr1, nestedArr2));
System.out.println("Arrays.deepEquals(): " + Arrays.deepEquals(nestedArr1, nestedArr2));
}
}
Output:
Arrays.equals(): false
Arrays.deepEquals(): true
In this example, Arrays.equals()
returns false
because it only compares the references of the inner arrays, which are different. However, Arrays.deepEquals()
returns true
because it recursively compares the contents of the inner arrays, finding that they are equal.
3.3. Comparing Multi-Dimensional Arrays
Arrays.deepEquals()
is particularly useful for comparing multi-dimensional arrays. It ensures that all levels of the array are compared, providing an accurate assessment of equality. Here’s an example:
import java.util.Arrays;
public class MultiDimensionalArrayComparison {
public static void main(String[] args) {
int[][] arr1 = {{1, 2}, {3, 4}};
int[][] arr2 = {{1, 2}, {3, 4}};
System.out.println("Arrays.deepEquals(): " + Arrays.deepEquals(arr1, arr2));
}
}
Output:
Arrays.deepEquals(): true
In this case, Arrays.deepEquals()
correctly identifies that the two multi-dimensional arrays are equal because it compares all the elements at each level of the array.
3.4. Considerations for Object Comparison
When using Arrays.deepEquals()
with arrays of objects, it’s important to ensure that the objects’ equals()
method is properly implemented. Arrays.deepEquals()
relies on the equals()
method to compare objects, so if the equals()
method is not defined or not implemented correctly, the comparison may not be accurate.
For example, if you have a custom class without an equals()
method:
import java.util.Arrays;
class MyObject {
int value;
public MyObject(int value) {
this.value = value;
}
}
public class CustomObjectComparison {
public static void main(String[] args) {
MyObject[] arr1 = {new MyObject(1), new MyObject(2)};
MyObject[] arr2 = {new MyObject(1), new MyObject(2)};
System.out.println("Arrays.deepEquals(): " + Arrays.deepEquals(arr1, arr2));
}
}
The output will be false
because the default equals()
method in Object
class compares object references, not their contents. To fix this, you need to override the equals()
method in your custom class:
import java.util.Arrays;
import java.util.Objects;
class MyObject {
int value;
public MyObject(int value) {
this.value = value;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
MyObject myObject = (MyObject) obj;
return value == myObject.value;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}
public class CustomObjectComparison {
public static void main(String[] args) {
MyObject[] arr1 = {new MyObject(1), new MyObject(2)};
MyObject[] arr2 = {new MyObject(1), new MyObject(2)};
System.out.println("Arrays.deepEquals(): " + Arrays.deepEquals(arr1, arr2));
}
}
Now the output will be true
because the equals()
method compares the value
field of the MyObject
instances.
4. Implementing Custom Array Comparison
While Arrays.equals()
and Arrays.deepEquals()
provide convenient ways to compare arrays, there are situations where you might need more control over the comparison process. In such cases, you can implement custom array comparison logic. This allows you to define specific criteria for equality, handle null values, and optimize the comparison for your specific data structures.
4.1. Why Implement Custom Comparison?
Implementing custom array comparison can be beneficial in several scenarios:
- Specific Equality Criteria: You may need to compare arrays based on specific criteria that are not covered by the default
equals()
method. - Null Handling: You may need to handle null values in a specific way, such as treating null as equal to an empty array.
- Performance Optimization: You can optimize the comparison process for your specific data structures, potentially improving performance.
- Custom Objects: When dealing with arrays of custom objects, you have full control over how equality is determined.
4.2. Basic Custom Comparison Example
Here’s a basic example of how to implement custom array comparison for integer arrays:
public class CustomArrayComparison {
public static boolean customEquals(int[] arr1, int[] arr2) {
if (arr1 == null && arr2 == null) {
return true;
}
if (arr1 == null || arr2 == null || arr1.length != arr2.length) {
return false;
}
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
int[] arr3 = {1, 2, 4};
System.out.println("arr1 equals arr2: " + customEquals(arr1, arr2)); // Output: true
System.out.println("arr1 equals arr3: " + customEquals(arr1, arr3)); // Output: false
}
}
In this example, the customEquals()
method first checks for null values and different lengths. Then, it iterates through the arrays, comparing corresponding elements. This approach provides a simple and efficient way to compare integer arrays.
4.3. Custom Comparison for Objects
When comparing arrays of objects, you need to ensure that the objects’ equals()
method is used correctly. Here’s an example of how to implement custom comparison for arrays of String
objects:
import java.util.Objects;
public class CustomStringArrayComparison {
public static boolean customEquals(String[] arr1, String[] arr2) {
if (arr1 == null && arr2 == null) {
return true;
}
if (arr1 == null || arr2 == null || arr1.length != arr2.length) {
return false;
}
for (int i = 0; i < arr1.length; i++) {
if (!Objects.equals(arr1[i], arr2[i])) {
return false;
}
}
return true;
}
public static void main(String[] args) {
String[] arr1 = {"apple", "banana", "cherry"};
String[] arr2 = {"apple", "banana", "cherry"};
String[] arr3 = {"apple", "banana", "date"};
System.out.println("arr1 equals arr2: " + customEquals(arr1, arr2)); // Output: true
System.out.println("arr1 equals arr3: " + customEquals(arr1, arr3)); // Output: false
}
}
In this example, the customEquals()
method uses Objects.equals()
to compare the String
objects. This ensures that null values are handled correctly and that the equals()
method of the String
class is used to compare the strings.
4.4. Implementing Custom Deep Comparison
For arrays containing nested arrays or objects, you can implement custom deep comparison logic. This involves recursively comparing the contents of the nested structures. Here’s an example:
import java.util.Arrays;
import java.util.Objects;
public class CustomDeepArrayComparison {
public static boolean customDeepEquals(Object[] arr1, Object[] arr2) {
if (arr1 == null && arr2 == null) {
return true;
}
if (arr1 == null || arr2 == null || arr1.length != arr2.length) {
return false;
}
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] instanceof Object[] && arr2[i] instanceof Object[]) {
if (!customDeepEquals((Object[]) arr1[i], (Object[]) arr2[i])) {
return false;
}
} else {
if (!Objects.equals(arr1[i], arr2[i])) {
return false;
}
}
}
return true;
}
public static void main(String[] args) {
Integer[] arr1 = {1, 2, 3};
Integer[] arr2 = {1, 2, 3};
Object[] nestedArr1 = {arr1};
Object[] nestedArr2 = {arr2};
System.out.println("customDeepEquals(): " + customDeepEquals(nestedArr1, nestedArr2));
}
}
In this example, the customDeepEquals()
method recursively calls itself to compare nested arrays. This ensures that the entire structure of the arrays is compared, providing an accurate assessment of equality.
5. Comparing Arrays of Primitive Types
When comparing arrays of primitive types in Java, you can use Arrays.equals()
or implement a custom comparison method. Primitive types include int
, double
, boolean
, char
, and others. Since primitive types are not objects, the comparison is straightforward and efficient.
5.1. Using Arrays.equals()
for Primitive Arrays
Arrays.equals()
works directly with arrays of primitive types, comparing the values of the elements. Here’s an example:
import java.util.Arrays;
public class PrimitiveArrayComparison {
public static void main(String[] args) {
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
int[] arr3 = {1, 2, 4};
System.out.println("arr1 equals arr2: " + Arrays.equals(arr1, arr2)); // Output: true
System.out.println("arr1 equals arr3: " + Arrays.equals(arr1, arr3)); // Output: false
double[] dArr1 = {1.0, 2.0, 3.0};
double[] dArr2 = {1.0, 2.0, 3.0};
System.out.println("dArr1 equals dArr2: " + Arrays.equals(dArr1, dArr2)); // Output: true
}
}
In this example, Arrays.equals()
correctly compares the integer and double arrays, returning true
if the arrays have the same elements in the same order.
5.2. Custom Comparison for Primitive Arrays
You can also implement custom comparison logic for primitive arrays if you need more control over the comparison process. Here’s an example:
public class CustomPrimitiveArrayComparison {
public static boolean customEquals(int[] arr1, int[] arr2) {
if (arr1 == null && arr2 == null) {
return true;
}
if (arr1 == null || arr2 == null || arr1.length != arr2.length) {
return false;
}
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] != arr2[i]) {
return false;
}
}
return true;
}
public static void main(String[] args) {
int[] arr1 = {1, 2, 3};
int[] arr2 = {1, 2, 3};
int[] arr3 = {1, 2, 4};
System.out.println("arr1 equals arr2: " + customEquals(arr1, arr2)); // Output: true
System.out.println("arr1 equals arr3: " + customEquals(arr1, arr3)); // Output: false
}
}
This example is similar to the basic custom comparison example, but it’s specifically tailored for integer arrays. You can adapt this approach for other primitive types as needed.
5.3. Performance Considerations
When comparing arrays of primitive types, Arrays.equals()
is generally efficient and sufficient for most use cases. However, if you need to optimize the comparison for specific scenarios, such as comparing large arrays or performing frequent comparisons, you might consider implementing custom comparison logic.
For example, if you know that the arrays are sorted, you can optimize the comparison by checking only the first and last elements. If they are different, the arrays are not equal. If they are the same, you can continue comparing the remaining elements.
6. Comparing Arrays of Objects
Comparing arrays of objects in Java requires careful consideration of how equality is defined for the objects. The Arrays.equals()
and Arrays.deepEquals()
methods rely on the equals()
method of the objects to determine if they are equal. Therefore, it’s essential to ensure that the equals()
method is properly implemented for your custom classes.
6.1. The Importance of equals()
Method
The equals()
method is used to compare two objects for equality. The default implementation in the Object
class compares object references, which means it only returns true
if the two variables refer to the same object in memory. To compare objects based on their contents, you need to override the equals()
method in your custom class.
Here’s an example of a custom class without an equals()
method:
class MyObject {
int value;
public MyObject(int value) {
this.value = value;
}
}
If you compare arrays of MyObject
instances using Arrays.equals()
or Arrays.deepEquals()
, the comparison will only check if the object references are the same, not if the objects have the same value
.
6.2. Implementing equals()
Method
To properly compare objects based on their contents, you need to override the equals()
method in your custom class. Here’s an example of how to implement the equals()
method for the MyObject
class:
import java.util.Objects;
class MyObject {
int value;
public MyObject(int value) {
this.value = value;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
MyObject myObject = (MyObject) obj;
return value == myObject.value;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}
In this example, the equals()
method first checks if the two objects are the same instance. Then, it checks if the other object is null or of a different class. Finally, it compares the value
field of the two objects. If the value
fields are equal, the method returns true
; otherwise, it returns false
.
It’s also important to override the hashCode()
method whenever you override the equals()
method. The hashCode()
method is used to generate a hash code for the object, which is used by hash-based collections such as HashMap
and HashSet
. If you don’t override the hashCode()
method, the hash-based collections may not work correctly.
6.3. Using Arrays.equals()
with Objects
When you have properly implemented the equals()
method in your custom class, you can use Arrays.equals()
to compare arrays of your objects. Here’s an example:
import java.util.Arrays;
import java.util.Objects;
class MyObject {
int value;
public MyObject(int value) {
this.value = value;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
MyObject myObject = (MyObject) obj;
return value == myObject.value;
}
@Override
public int hashCode() {
return Objects.hash(value);
}
}
public class ObjectArrayComparison {
public static void main(String[] args) {
MyObject[] arr1 = {new MyObject(1), new MyObject(2), new MyObject(3)};
MyObject[] arr2 = {new MyObject(1), new MyObject(2), new MyObject(3)};
System.out.println("arr1 equals arr2: " + Arrays.equals(arr1, arr2)); // Output: true
}
}
In this example, Arrays.equals()
correctly compares the arrays of MyObject
instances because the equals()
method is properly implemented in the MyObject
class.
6.4. Using Arrays.deepEquals()
with Objects
Arrays.deepEquals()
also relies on the equals()
method of the objects to determine if they are equal. Therefore, you need to ensure that the equals()
method is properly implemented for your custom classes when using Arrays.deepEquals()
with arrays of objects.
7. Comparing Arrays with Null Values
Handling null values is an important consideration when comparing arrays in Java. Null values can occur in arrays of objects, and you need to decide how to treat them during the comparison process. You can handle null values using Arrays.equals()
, Arrays.deepEquals()
, or custom comparison logic.
7.1. Null Handling with Arrays.equals()
Arrays.equals()
handles null values by considering two null elements as equal. If both arrays have a null element at the same index, Arrays.equals()
will treat those elements as equal. Here’s an example:
import java.util.Arrays;
public class NullArrayComparison {
public static void main(String[] args) {
String[] arr1 = {"apple", null, "cherry"};
String[] arr2 = {"apple", null, "cherry"};
String[] arr3 = {"apple", "banana", "cherry"};
System.out.println("arr1 equals arr2: " + Arrays.equals(arr1, arr2)); // Output: true
System.out.println("arr1 equals arr3: " + Arrays.equals(arr1, arr3)); // Output: false
}
}
In this example, Arrays.equals(arr1, arr2)
returns true
because both arrays have a null element at the same index. Arrays.equals(arr1, arr3)
returns false
because the arrays have different elements.
7.2. Null Handling with Arrays.deepEquals()
Arrays.deepEquals()
also handles null values by considering two null elements as equal. If both arrays have a null element at the same index, Arrays.deepEquals()
will treat those elements as equal. The behavior is the same as Arrays.equals()
.
7.3. Custom Null Handling
If you need more control over how null values are handled, you can implement custom comparison logic. This allows you to define specific criteria for equality when null values are encountered.
Here’s an example of how to implement custom null handling:
import java.util.Objects;
public class CustomNullArrayComparison {
public static boolean customEquals(String[] arr1, String[] arr2) {
if (arr1 == null && arr2 == null) {
return true;
}
if (arr1 == null || arr2 == null || arr1.length != arr2.length) {
return false;
}
for (int i = 0; i < arr1.length; i++) {
if (arr1[i] == null && arr2[i] == null) {
continue; // Treat null values as equal
}
if (!Objects.equals(arr1[i], arr2[i])) {
return false;
}
}
return true;
}
public static void main(String[] args) {
String[] arr1 = {"apple", null, "cherry"};
String[] arr2 = {"apple", null, "cherry"};
String[] arr3 = {"apple", "banana", "cherry"};
System.out.println("arr1 equals arr2: " + customEquals(arr1, arr2)); // Output: true
System.out.println("arr1 equals arr3: " + customEquals(arr1, arr3)); // Output: false
}
}
In this example, the customEquals()
method explicitly checks for null values and treats two null elements as equal. This provides more control over the comparison process.
7.4. Considerations for Null Handling
When handling null values, you need to consider the specific requirements of your application. In some cases, you might want to treat null values as equal to empty strings or other default values. In other cases, you might want to treat null values as unequal to any other value. The best approach depends on the specific context and the meaning of null values in your data.
8. Performance Considerations for Array Comparison
The performance of array comparison can be an important consideration, especially when dealing with large arrays or performing frequent comparisons. The Arrays.equals()
and Arrays.deepEquals()
methods provide reasonable performance for most use cases, but there are situations where you might need to optimize the comparison process.
8.1. Arrays.equals()
Performance
Arrays.equals()
has a time complexity of O(n), where n is the length of the arrays. This means that the time it takes to compare the arrays is proportional to the number of elements in the arrays. For most use cases, this is acceptable.
8.2. Arrays.deepEquals()
Performance
Arrays.deepEquals()
also has a time complexity of O(n), but it can be slower than Arrays.equals()
because it needs to recursively compare nested arrays and objects. The actual performance depends on the complexity of the nested structures and the equals()
method of the objects.
8.3. Optimizing Array Comparison
There are several ways to optimize array comparison:
- Check Length First: Before comparing the elements, check if the arrays have the same length. If they don’t, they are not equal, and you can return
false
immediately. - Short-Circuiting: If you find a difference between the elements, you can return
false
immediately without comparing the remaining elements. - Sorted Arrays: If you know that the arrays are sorted, you can optimize the comparison by checking only the first and last elements. If they are different, the arrays are not equal. If they are the same, you can continue comparing the remaining elements.
- Hashing: You can use hashing techniques to compare arrays more efficiently. For example, you can calculate a hash code for each array and compare the hash codes. If the hash codes are different, the arrays are not equal. However, you need to handle hash collisions, which can occur when two different arrays have the same hash code.
- Parallel Comparison: For very large arrays, you can use parallel processing to compare the arrays in parallel. This can significantly improve the performance, especially on multi-core processors.
8.4. Benchmarking
The best way to determine if your optimizations are effective is to benchmark your code. Benchmarking involves measuring the execution time of your code for different inputs and comparing the results. You can use benchmarking tools such as JMH (Java Microbenchmark Harness) to perform accurate and reliable benchmarks.
9. Practical Examples and Use Cases
To illustrate the practical applications of array comparison in Java, let’s explore some real-world examples and use cases. These examples will demonstrate how to use the different comparison methods and techniques discussed in this article.
9.1. Testing Frameworks
In testing frameworks like JUnit, array comparison is essential for verifying the correctness of methods that return arrays. Here’s an example:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
public class ArrayTest {
@Test
public void testArrayEquality() {
int[] expected = {1, 2, 3};
int[] actual = getArrayFromMethod(); // Assume this method returns {1, 2, 3}
assertArrayEquals(expected, actual);
}
private int[] getArrayFromMethod() {
return new int[]{1, 2, 3};
}
}
In this example, assertArrayEquals()
is used to compare the expected array with the actual array returned by the getArrayFromMethod()
method. If the arrays are not equal, the test will fail.