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 without built-in functions





In this blog, we will see how to compare two arrays in java without in-built functions. This will be asked in the most of the interview questions.

In the below snippet, we have done the following:

  1. Created two arrays of same datatype and assigned those with some elements
  2. Created a helper class with parameters  of these two arrays
  3. Inside the helper class, to compare the arrays we have done the following
    • Firstly, we compared both the declared arrays length are equal or not using arrayname.length() and returned the value
    • Then, we have iterated a for loop based on array.length() and then compared the arrays index is equal or not and returned the value.
  4. In the main method, we mentioned an If condition which will validate the result based on the returned value.
package Javaprograms;

public class CompareArraysWithoutInBuiltFunctions {

	public static void main(String[] args) {
		String a[] = {"apple","bat","cat"};
		String b[] = {"apple","bat","catt"};
		
		if(arraysCompareCheck(a, b)){
			System.out.println("Both arrays are equal");
		}
		else
		{
			System.out.println("Both arrays are not equal");
		}
		
	}
		public static boolean arraysCompareCheck(String a[], String b[]){
			if(a.length!=b.length){
				return false;
			}
			else
			{
				for(int i=0;i<a.length;i++)
				{
					if(a[i]!=b[i])
					{
						return false;
					}	
				}			
			}
			return true;		
		}
	}
Output:
--------
Both arrays are not equal





Here, It’s better to use the Object class as a parameter as it is the super class which can inherit all the datatypes and supports whatever the array’s datatype is.

Below is the snippet which uses the Object class as a parameter:

If you’re using an integer array with an object class parameter, Make sure to use the derived datatype Integer instead of Int

package Javaprograms;

public class CompareArraysWithoutInBuiltFunctions {

	public static void main(String[] args) {
		//String a[] = {"apple","bat","cat"};
		//String b[] = {"apple","bat","catt"};
		
		Integer a[] = {1,2,19};
		Integer b[] = {1,2,19};
		
		if(arraysCompareCheck(a, b)){
			System.out.println("Both arrays are equal");
		}
		else
		{
			System.out.println("Both arrays are not equal");
		}
		
	}
		public static boolean arraysCompareCheck(Object a[], Object b[]){
			if(a.length!=b.length){
				return false;
			}
			else
			{
				for(int i=0;i<a.length;i++)
				{
					if(a[i]!=b[i])
					{
						return false;
					}	
				}			
			}
			return true;		
		}
	}
Output:
-------
Both Arrays are equal





Please watch the Youtube video for better understanding.



Share this post: on Twitter on Facebook

How to Compare Two Arrays in Java using built-in functions How to Read Data From Properties File in Java

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

Java IQs

How to Compare Two Arrays in Java using 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