- Java Arrays — equals(char[], char[]) Method
- Declaration
- Parameters
- Return Value
- Exception
- Java Arrays equals(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex) Method
- Description
- Declaration
- Parameters
- Return Value
- Exception
- Example 1
- Output
- Example 2
- Output
- Example 3
- Output
- Java, Checking to see if two char arrays are equal
- 2 Answers 2
Java Arrays — equals(char[], char[]) Method
The Java Arrays equals(char[] a, char[] a2) method returns true if the two specified arrays of chars are equal to one another. Two arrays are equal if they contain the same elements in the same order. Two array references are considered equal if both are null.
Declaration
Following is the declaration for java.util.Arrays.equals() method
public static boolean equals(char[] a, char[] a2)
Parameters
- a − This is the array to be tested for equality.
- a2 − This is the other array to be tested for equality.
Return Value
This method returns true if the two arrays are equal, else false
Exception
Java Arrays equals(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex) Method
Description
The Java Arrays equals(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex) method returns true if the two arrays of chars in the given ranges are equal to each other. In case of any array null, a NullPointerException is thrown.
Declaration
Following is the declaration for java.util.Arrays.compare(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex) method
public static boolean equals(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex)
Parameters
- a − This is the first array to be tested for equality.
- aFromIndex − This is the index of the first element (inclusive) of first array to be tested for equality.
- aToIndex − This is the index of the last element (exclusive) of first array to be tested for equality.
- b − This is the second array to be tested for equality.
- bFromIndex − This is the index of the first element (inclusive) of second array to be tested for equality.
- bToIndex − This is the index of the last element (exclusive) of second array to be tested for equality.
Return Value
This method returns true if the two arrays, over the specified ranges, are equal.
Exception
- IllegalArgumentException − if aFromIndex > aToIndex or if bFromIndex > bToIndex
- ArrayIndexOutOfBoundsException − if aFromIndex < 0 or aToIndex >a.length or if bFromIndex < 0 or bToIndex >b.length
- NullPointerException − if either array is null
Example 1
The following example shows the usage of Java Arrays equals(char[], char[]) method. First, we’ve created three arrays of chars, and compared them using equals(char[], char[]) method. As per the result, the comparison is printed.
package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo < public static void main(String[] args) < // initializing three char arrays char[] arr1 = new char[] < 'A', 'D' , 'F' >; char[] arr2 = new char[] < 'B', 'E' , 'F' >; char[] arr3 = new char[] < 'A', 'D' , 'F' >; // comparing arr1 and arr2 boolean retval = Arrays.equals(arr1, arr2); System.out.println("arr1 and arr2 equal: " + retval); // comparing arr1 and arr3 boolean retval2 = Arrays.equals(arr1, arr3); System.out.println("arr1 and arr3 equal: " + retval2); > >
Output
Let us compile and run the above program, this will produce the following result −
arr1 and arr2 equal: false arr1 and arr3 equal: true
Example 2
The following example shows the usage of Java Arrays equals(char[], int, int, char[], int, int) method. First, we’ve created three arrays of chars, and compared them using equals(char[], int, int, char[], int, int) method. As per the result, the comparison is printed.
package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo < public static void main(String[] args) < // initializing three char arrays char[] arr1 = new char[] < 'A', 'D' , 'F' >; char[] arr2 = new char[] < 'B', 'E' , 'F' >; char[] arr3 = new char[] < 'A', 'D' , 'F' >; // comparing arr1 and arr2 boolean retval = Arrays.equals(arr1, 0, 2, arr2, 0, 2); System.out.println("arr1 and arr2 equal: " + retval); // comparing arr1 and arr3 boolean retval2 = Arrays.equals(arr1, 0, 2, arr3, 0, 2); System.out.println("arr1 and arr3 equal: " + retval2); > >
Output
Let us compile and run the above program, this will produce the following result −
arr1 and arr2 equal: false arr1 and arr3 equal: true
Example 3
The following example shows the usage of Java Arrays equals(char[], int, int, char[], int, int) method. First, we’ve created three arrays of chars, and compared their sub-arrays using equals(char[], int, int, char[], int, int) method. As per the result, the comparison is printed.
package com.tutorialspoint; import java.util.Arrays; public class ArrayDemo < public static void main(String[] args) < // initializing three char arrays char[] arr1 = new char[] < 'A', 'D' , 'F' >; char[] arr2 = new char[] < 'B', 'E' , 'F' >; char[] arr3 = new char[] < 'A', 'D' , 'F' >; // comparing arr1 and arr2 boolean retval = Arrays.equals(arr1, 2, 3, arr2, 2, 3); System.out.println("Last element of arr1 and arr2 equal: " + retval); // comparing arr1 and arr3 boolean retval2 = Arrays.equals(arr1, 2, 3, arr3, 2, 3); System.out.println("Last element of arr1 and arr3 equal: " + retval2); > >
Output
Let us compile and run the above program, this will produce the following result −
Last element of arr1 and arr2 equal: true Last element of arr1 and arr3 equal: true
Java, Checking to see if two char arrays are equal
I have two character arrays in Java: orig_array and mix_array . I need to check if they are not equal. Here is what I have so far:
sample data orig_team=one mix_team=neo while(!Arrays.equals(mix_team, orig_team)) < if (Arrays.equals(mix_team, orig_team)) < System.out.println("congradulations! you did it"); System.exit(0); >else < System.out.println("enter the index"); Scanner scn = new Scanner(System.in); int x = scn.nextInt(); int y = scn.nextInt(); char first=mix_team[x]; char second=mix_team[y]; mix_team[x]=second; mix_team[y]=first; for (int i = 0; i < mix_team.length; i = i + 1) < System.out.print(i); System.out.print(" "); >System.out.println(); System.out.println(mix_team); > >
2 Answers 2
The while loop’s block is only executed when the two arrays are not equal, so starting that block with the same equality check makes no sense. In other words, the line:
if (Arrays.equals(mix_team, orig_team))
You essentially have the following loop:
The code inside the while loop will only run if something evaluates to true . Thus, the value of !something will always be false, and the if statement’s contents will not be run.
while (!Arrays.equals (mix_team, orig_team)) < System.out.println("enter the index"); Scanner scn = new Scanner(System.in); int x = scn.nextInt(); int y = scn.nextInt(); char first=mix_team[x]; char second=mix_team[y]; mix_team[x]=second; mix_team[y]=first; for (int i = 0; i < mix_team.length; i = i + 1) < System.out.print(i); System.out.print(" "); >System.out.println(); System.out.println(mix_team); > System.out.println("congratulations! you did it"); System.exit(0);
By the way, you don’t need to create a scanner each time. A better way to do it would be to declare the scanner before the while loop (basically move the initialization line up two lines).