Comparing two integer arrays in Java
public static void compareArrays(int[] array1, int[] array2) {
boolean b = true;
if (array1 != null && array2 != null){
if (array1.length != array2.length)
b = false;
else
for (int i = 0; i < array2.length; i++) {
if (array2[i] != array1[i]) {
b = false;
}
}
}else{
b = false;
}
System.out.println(b);
}
How to compare int arrays in Java?
A few things to note here:
==
compares the references, not the values . . . that is, you are asking whether these two arrays are the same exact instance, not whether they contain the same values.- The fact that you are using
==
means you may not know about theequals()
method onObject
. This is not the method you'll need to solve this current problem, but just be aware that in general, when you compare the values of two objects, you should be usingobj1.equals(obj2)
, notobj1 == obj2
. Now==
does work with primitives likeint
(e.g. plain oldx == 3
and so on), so maybe that's why you were using it, but I just wanted to make sure you were aware ofequals()
vs.==
. - In the old old days (pre-1998), you would have to compare each element pair of the two arrays. Nowadays, you can just use that static
Arrays.equals()
method on thejava.util.Arrays
class. This method is overloaded for all the primitive types (using==
under the hood for each element pair) and forObject
(where it will most definitely useequals()
for each pair.)
How do you compare two int arrays elements separately in Java and see which is the greater element?
You need 2 variables that holds each ones score, increment the one that has the bigger card for each round, at the end find the biggest score
int[] playerOne = {10, 6, 8, 9, 7, 12, 7};
int[] playerTwo = {7, 6, 9, 5, 2, 8, 12};
int scorePlayerOne = 0, scorePlayerTwo = 0;
for (int i = 0; i <= playerOne.length - 1; i++) {
if (playerOne[i] < playerTwo[i]) {
scorePlayerTwo++;
} else if (playerTwo[i] < playerOne[i]) {
scorePlayerOne++;
}
}
if (scorePlayerOne < scorePlayerTwo) {
System.out.println("Player Two wins");
} else if (scorePlayerTwo < scorePlayerOne) {
System.out.println("Player One wins");
} else {
System.out.println("Draw");
}
How can I compare two integers in one array in Java?
you are expecting it to check just the next element, but you are checking next element to the end
This is what is happening with your current code
1,5,6,4,3,10
5 > 4
5 > 3
6 > 4
6 > 3
4 > 3
and so count = 5
remove inner loop and change condition to
if (t[i] < t[i+1]){
sum += 1;
}
Compare two integer arrays using Java Stream
You may do it like so,
List<Boolean> equalityResult = IntStream.range(0, a.length).mapToObj(i -> a[i] == b[i])
.collect(Collectors.toList());
Precondition: both the arrays are of same size.
Compare two integer arrays for similar values
Using streams you can do something like:
import java.util.Arrays;
public class Test {
public static void main(String[] args){
int[] x = {0,1,4,7,8,9};
System.out.println(matchArrays(x));
}
public static int matchArrays (int[] x) {
int[] y = {1,2,3,5,7,7,8};
return (int)Arrays.stream(x).filter(i->Arrays.stream(y).anyMatch(j->j==i)).count();
}
}
Java: how to compare two int[] arrays for non duplicate elements?
You can use binary search to compare arrays and find differences. You need to do that comparison as two-way(<- ->) like:
array1 --> array2 and array2 --> array1
Because you need to sum up differences of sets. Let A and B our sets, we need to find:
(A-B) U (B-A)
The binary search solution is below. Complexity of that algorithm is O(log n)
private static int getDifferenceBetweenTwoArray(int[] array1 , int[] array2)
{
int differenceCount = 0;
//if you dont want to sort your original arrays, create temporary arrays
int temp1[] = Arrays.copyOf(array1 , array1.length);
int temp2[] = Arrays.copyOf(array2 , array2.length);
Arrays.sort(temp1);
Arrays.sort(temp2);
for(Integer i : temp1)
{
if(Arrays.binarySearch(temp2, i) < 0)
differenceCount++;
}
for(Integer i: temp2)
{
if(Arrays.binarySearch(temp1, i) < 0)
differenceCount++;
}
return differenceCount;
}
Related Topics
Gson - Convert from JSON to a Typed Arraylist<T>
Android/Java -- Post Simple Text to Facebook Wall
Converting a String to an Integer on Android
How to Add a Shadow and a Border on Circular Imageview Android
Retrofit2 Android: Expected Begin_Array But Was Begin_Object at Line 1 Column 2 Path $
Android.Util.Androidruntimeexception: Requestfeature() Must Be Called Before Adding Content
Background Service for Android Oreo
Why I Cant Use the Populateviewholder Override Method
Onclicklistener Not Responding
How to View My Realm File in the Realm Browser
How to Create a Bks (Bouncycastle) Format Java Keystore That Contains a Client Certificate Chain
Ionic Build Android Error When Downloading Gradle
How to Use Vectordrawable in Buttons and Textviews Using Android:Drawableright
Android - Setting a Timeout for an Asynctask
Getting Java.Net.Sockettimeoutexception: Connection Timed Out in Android
Android: How to Gain Root Access in an Android Application