0

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?

Erik Vorcov
  • 51
  • 1
  • 8

3 Answers3

2

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
azurefrog
  • 10,785
  • 7
  • 42
  • 56
1

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.

KSFT
  • 1,774
  • 11
  • 17
  • hm when i do something like implementing result = Array.equals(diffValue, new int[] {0}) ? PASS : FAIL; it seems to still pass FAIL. for instance diffValue = {0} will be the result of giving a1 ={4} and a2 = {4}; which is {0} but it still fails after i have added result = Array.equals(diffValue, new int[] {0}) ? PASS : FAIL; @KSFT – Erik Vorcov Mar 04 '15 at 01:03
  • @ErikVorcov Try printing `diffValue` to make sure it's `{0}`. – KSFT Mar 04 '15 at 01:05
  • interesting, i wouldve thought maybe arrays was imported for you, and good idea – Erik Vorcov Mar 04 '15 at 01:06
-1

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.

Community
  • 1
  • 1
tar
  • 1,538
  • 1
  • 20
  • 33
  • This is wrong. It won't always resolve to `true`. In fact, it will always resolve to `false`, because `diffValue` can't be the same object as the new int array being created. What do you mean by "REALLY the same thing, not simply equivalent"? – KSFT Mar 04 '15 at 00:47
  • I made a mistake leaving that word unchanged when I was rewriting the answer (had changed "never resolve to true" to "always resolve to false"). Thank you for correcting me. – tar Mar 04 '15 at 04:03