Check equality of two arrays using Java
Learning new technologies to enhance our career in the world of technologies, especially as a tester growing our career in testing we must be fundamentally strong with our basic concept of programming languages.
I know as a tester you have mastered it all and you all must be thinking why I am talking on this point? Is it helpful? So let me explain myself, those who know the basic logic programs that we all have faced during our interviews and we all use that logic in our day-to-day automation scrip building we all have mastered this. But for those who are new to the world of programming and find it difficult to understand the basic concept and don't know how to construct logic to solve problems so for them in the upcoming days, we will learn some basic and most frequently asked logical programs to help you with your interview preparation.
Here, we will be using Java language to solve the problems. But don't worry the logic is the same it is just a matter of syntax. In the upcoming days, we will be solving some problems related to arrays and I will post that with an explanation so that you can have a better understanding.
Our first array program is about the equality of two arrays. In this program, we will check if the two arrays are the same or not. So we can achieve this by following three approaches as listed below.
For a better understanding of the logic please check out the explanation so that you can also learn how to build logic to solve a problem.
Problem statement:
Verify the equality of listed arrays. (Here we are using Java language)
Solution:
package array; | |
import java.util.Arrays; | |
public class Arrays_Equality { | |
public void equality() | |
{ | |
int [] a = new int []{2,5,7,8,9}; | |
int [] b = new int []{2,5,7,8,9}; | |
//Approach1 | |
System.out.println(Arrays.equals(a, b)); //Approach 2 | |
for (int i=0; i<a.length;i++) | |
{ | |
for (int j=0; j<b.length;j++) | |
{ | |
if (a[i]==b[j]) | |
{ | |
} | |
} | |
} | |
System.out.println("equal array"); //Approach 3 | |
if (a == b) | |
System.out.println("Arrays are equal"); | |
else | |
System.out.println("Not equal"); | |
} | |
public static void main(String[] args) { | |
Arrays_Equality j = new Arrays_Equality(); | |
j.equality(); | |
} | |
} |
Explanation:
In approach one we are using the dot equals method so it will return a boolean value and we can see that one in the output window.
In approach two here, we have listed down the two arrays that have an int data type, and then we are using a for loop that will check the elements present in both arrays by their index numbers. For example, at a[0] the element present is 2 and for b[0] The element present is also 2 then it will increase the value of i and j and will go for the next index, and again it will check whether the two elements are equal or not.
In approach three we are using if else conditional statements with == operator so if the condition satisfies the if conditional statement then it will print Arrays are equal otherwise it will print Not equal.
Thanks, I have been searching for info about this subject matter for ages and yours is the best I have found so far. automation testing
ReplyDelete