Selenium Webdriver Appium Complete TutorialSelenium Webdriver Appium Complete Tutorial
Automation Testing
  • Tools
    • Selenium
      • Selenium Java Tutorial
      • Selenium C# Tutorial
    • Appium
      • Appium Java Tutorial
      • Appium C#Tutorial
    • Katalon
  • Trainings
  • TestNG
  • Reports
    • Extent Reports
      • Extent Reports – Java
      • Extent Reports – Java -Version3
      • Extent Reports – C#
    • Vigo Reports
    • AT Excel Report
  • Excel
    • Apache POI – Java
    • Excel With C#
  • Interview Questions
    • Selenium Interview Questions
    • Java Interview Questions
    • C# Interview Questions
  • Demo Site
  • Practice Site
  • More…
    • AutoIt
    • Sikuli
    • Robot Class
    • File Upload
    • ScreenShot
      • AShot
      • ShutterBug
  • About
November 16, 2019

How to Compare Two Arrays in Java using built-in functions




In this blog, we will learn how to compare two arrays in Java using built-in functions. This will be asked in the most of the interviews. This can be implemented in the below way:

In the below snippet, we have done the following:

  1. Created two arrays and assigned some elements in it.
  2. Using built-in function Arrays.equals method we can compare our two arrays and validate them accordingly.
  3. Here, we have declared the two arrays differently, one with 3 elements and the other array with 4 elements
  4. Validating them using If statement and the built-in function as mentioned above





package Javaprograms;

import java.util.Arrays;

public class CompareArrays {

	public static void main(String[] args) {
		int a[] = {1,3,5};
		int b[] = {1,3,4,5};
		
		if(Arrays.equals(a, b))
		{
			System.out.println("Both the Arrays are EQUAL");
		}
		else
		{
			System.out.println("Both the Arrays are not EQUAL");
		}
	}

}
Output:
-------
Both the Arrays are not EQUAL





In the below snippet, we have done the following:

  1. using the same above arrays, But in this instance we have used the equal elements and equal values in their indices of those arrays to see the result as Equal.
package Javaprograms;

import java.util.Arrays;

public class CompareArrays {

	public static void main(String[] args) {
		int a[] = {1,3,5};
		int b[] = {1,3,5};
		
		if(Arrays.equals(a, b))
		{
			System.out.println("Both the Arrays are EQUAL");
		}
		else
		{
			System.out.println("Both the Arrays are not EQUAL");
		}
	}

}

Output:
-------
Both the Arrays are EQUAL

Please watch the Youtube video for better understanding.



Share this post: on Twitter on Facebook

How to Remove Whitespaces from a String in Java How to Compare Two Arrays in Java without built-in functions

Related Posts

How To SWAP Two Numbers in Java Without using Temp Variable

Java IQs

How to SWAP Two Numbers in Java Without Temp variable or Without Third variable

How to Swap Two Numbers in Java with Temp Variable

Java IQs

How to SWAP Two Numbers in Java using Temp Variable

How to Read Properties file in Java

Java IQs, Uncategorized

How to Read Data From Properties File in Java

Compare two arrays in java with out inbuilt functions

Java IQs

How to Compare Two Arrays in Java without built-in functions

Remove White Spaces From A String in Java

Java IQs

How to Remove Whitespaces from a String in Java

Sum of The digits in a number in Java

Java IQs

How to Perform Sum of the Digits in a Given Number in Java

How To Reverse A String in Java

Java IQs

How to Reverse a String in Java

Newsletter

Recent Posts

  • TAKING WEB ELEMENT SCREENSHOT IN SELENIUMHow to Capture WebElement Screenshot in Selenium Webdriver using selenium 4
    December 15, 2019
  • How To SWAP Two Numbers in Java Without using Temp VariableHow to SWAP Two Numbers in Java Without Temp variable or Without Third variable
    December 8, 2019
  • How to Swap Two Numbers in Java with Temp VariableHow to SWAP Two Numbers in Java using Temp Variable
    December 1, 2019
  • How to Read Properties file in JavaHow to Read Data From Properties File in Java
    November 27, 2019
  • Compare two arrays in java with out inbuilt functionsHow to Compare Two Arrays in Java without built-in functions
    November 16, 2019
© Selenium Webdriver Appium Complete Tutorial 2025