Reputation: 59
I am writing a program here that takes two int[] arrays and subtracts each element from each other and stores the result in a new array.
this sounds pretty sound and i know my math is right seeing as its simple subtraction..
my method is here,
public int[] diff(int[] nums1, int[] nums2)
{
int[] diffArray = new int [nums1.length];
int tempValue = 0;
int tempValue2 = 0;
int subtract = 0;
if (nums1.length != nums2.length)
{
diffArray = new int[0];
}
else
{
for (int i = 0; i < nums1.length && i < nums2.length; ++i)
{
tempValue = nums1[i];
tempValue2 = nums2[i];
System.out.println("temp value 1: " + tempValue); //just me trying to debug
System.out.println("temp value 2: " + tempValue2); //just me trying to debug
subtract = tempValue2 - tempValue;
System.out.println("Subtracted :" + subtract); //just me trying to debug
diffArray[i] = subtract;
}
}
return diffArray;
}
to test this i wrote a couple small pieces of code in my driver class.
The problem is, i made those 3 debug print statements in my for loop to see if i had the right numbers and everything is correct it just seems like its not storing it in diffArray?
for example,
//Array of 1 element each : r(0)
givenArray = new int[] {4};
givenArray2 = new int[] {4};
diffValue = srvObj.diff(givenArray, givenArray2);
result = (diffValue == new int[] {0}) ? PASS : FAIL;
System.out.println(testNum + ": " + result);
++testNum;
//Array of multi-elements each that are diff : r({6, 10, 37})
givenArray = new int[] {4, 5, 3};
givenArray2 = new int[] {10, 15, 30};
diffValue = srvObj.diff(givenArray, givenArray2);
result = (diffValue == new int[] {6, 10, 37}) ? PASS : FAIL;
System.out.println(testNum + ": " + result);
++testNum;
note : i am subtracting array2 from array1 not the other way.
however i keep getting FAIL every time? i can see in my print statements its done the math for each element
Surely there must be a problem when i try to give diffArray[i] the resulted subtraction?
Upvotes: 0
Views: 92
Reputation: 1568
Your test condition is wrong - diffValue == new int[] {6, 10, 37}
will always resolve to false
because the Java equality operator checks that the two variables are REALLY the same thing, not simply equivalent. Please see Comparing two integer arrays in java for ideas on comparing int arrays.
Upvotes: -1
Reputation: 1774
In Java (and a lot of other languages), arrays are objects, so ==
won't work to compare them. It compares the references, so it checks if they're the same object, not whether they contain the same elements:
int[] a = new int[] {1,2,3};
a == a; //true
int[] b = new int[] {1,2,3};
a == b; //false
There's a method, though, Arrays.equals(), that can compare them:
result = Arrays.equals(diffValue, new int[] {0}) ? PASS : FAIL;
You need to import Arrays at the beginning of your program, though:
import java.util.Arrays;
It looks like your diff()
is working, though.
Upvotes: 1
Reputation: 10945
You are checking for array equality incorrectly here (and your other tests):
result = (diffValue == new int[] {0}) ? PASS : FAIL;
In Java the ==
operator checks for reference equality when applied to objects, and the new array you create in this statement will never be the same object as diffValue
.
What you need to do is to use the equals()
method in the Arrays
class, which checks for value equality.
A quick example:
int[] arr1 = { 0 };
int[] arr2 = { 0 };
System.out.println(arr1 == arr2); // false
System.out.println(Arrays.equals(arr1, arr2)); // true
Upvotes: 2