Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

A.length() and b.length() java

  1. Nov 16, 2004 #1
    Code (Text):
     public class check
    {
        public boolean isDifferent(int [] a, int [] b)
        {
         for(int i=0; i < a.length; i++)    //loop through your a array
        {
            for(int j=0;j<b.length;j++)     //next loop through your b array
            {
                if( a[i] != b[j] )  // check for inequality
                {  
                    return true;
                }
            }
       }

       return false;
    }
        }
     
    is this correct?
     
  2. jcsd
  3. Nov 17, 2004 #2
    Yes it is, except that you'd have to use a.length() and b.length(). I have just a comment, that it might be better practice to capitalize class names. Use Check instead of check.
     
  4. Nov 17, 2004 #3

    NoTime

    User Avatar
    Science Advisor
    Homework Helper

    Errr. Probably not.
    Your code checks A[n] = B[0->n] for all n in A.
    I doubt that that was your intent.
    The only way you could get false is if all elements had the same value.
     
    Last edited: Nov 17, 2004
  5. Nov 17, 2004 #4
    how would i correct it?
     
  6. Nov 17, 2004 #5

    NoTime

    User Avatar
    Science Advisor
    Homework Helper

    What are you trying to do?
     
  7. Nov 17, 2004 #6
    i am trying to return true if any elemtns in a are in b and false if it is not
     
  8. Nov 17, 2004 #7

    NoTime

    User Avatar
    Science Advisor
    Homework Helper

    Change != to ==
     
  9. Nov 18, 2004 #8
    No, the function isDifferent() returns true when the two arrays are different, so the code is correct.
     
  10. Nov 18, 2004 #9

    NoTime

    User Avatar
    Science Advisor
    Homework Helper

    The OPs desired results seem different than what you say :confused:
     
  11. Nov 20, 2004 #10
    first of all,
    the function is named as isDifferent()
    which means it is supposed to check whether the arrays are different or not ... in which case the program is correct.

    Even if it were supposed to check whether the arrays are same (if it is so , please change the name to isSame() to avoid confusion) , it is not right to change != to == since it might simply check the first element and if they are equal it will return which is not correct .... infact u have to exchange return true and return false ... thats all ...

    -- AI
     
  12. Nov 20, 2004 #11
    no i think it would work, i just tested it
     
  13. Nov 20, 2004 #12
    It will always return true, unless the arrays a[] and b[] are both degenerate, and to most intents and purposes, useless.

    Imagine a[0] = 3, then unless b[0] == 3, b[1] == 3, b[2] == 3, etc., the function will return true.

    If all the elements of b[] are equal to 3, the function next checks a[1] against b[0] (which we already know is 3) so unless a[1] == 3 the function returns true, and so on.

    So unless all the elements of both a[] and b[] contain exactly the same value (the number '3' in my example) the function returns true.

    If you really want to know if the arrays a[] and b[] are different to each other, you need to write a different function.
     
  14. Nov 20, 2004 #13
    Here's a better version, though I've not tested it.

    Code (Text):
     public class check
    {
        public boolean isDifferent(int[] a, int[] b)
        {
            if (a.length() != b.length()) return true;

            for (int i = 0; i < a.length(); i++)    //loop through your a array
                if (a[i] != b[i])  // check for inequality
                    return true;

            return false; // returns false if both arrays are same length and all elements match
        }
    }
     
     
  15. Nov 20, 2004 #14
    There's an easier way!

    The way you're doing it would work, too, though.

    Now, assuming that the .equals() method works like I think it should for int[], you could simply have one line.

    return !(a.equals(b));

    if not, though, I would use something like ceptimus's code, except it is ".length" and not ".length()".
     
  16. Nov 21, 2004 #15
    The Idiot,
    no that wont work either.

    int a[] = {1,2,3};
    int b[] = {1,2,3};
    System.out.println(a.equals(b));
    This will print false....
    Similarly,
    int a[] = {1,2,3};
    int b[] = {1,3,4};
    System.out.println(a.equals(b));
    This also will print false....

    However,
    int a[] = {1,2,3};
    int b[];
    b=a;
    System.out.println(a.equals(b));
    This will print true ...

    Can u see what it is doing?

    Back to the problem,
    If we note carefully , ceptimus has the same program as courtrigrad had except he added the vital condition of equal lengths to it....

    courtrigrad,
    replacing != with == would not work

    If a = {1,2,3} and b={1,3,4}
    replacing != with == will give true whereas it should be false.

    The only way of achieving the "isSame()" function is exchanging the positions of return true and return false.

    -- AI
    P.S -> yes it is a.length and not a.length() as The Idiot mentions. (No pun intended :biggrin: )
     
    Last edited: Nov 21, 2004
  17. Nov 21, 2004 #16
    ...Which is why I said "assuming it works the way I think it should."
     
  18. Nov 22, 2004 #17
    Arent u glad u got it cleared? :wink:

    -- AI
    Its always good to stand on one side and say this grass is greener than to stand on the fence and decide which grass is greener.
     
  19. Nov 22, 2004 #18
    guys thanks for the help but i think this is not what i was looking for. the program returns true if all of the elements in the first array are in the second array regardless of order.


    Example

    {1,2,3}
    {3,2,1,5,6,7}

    returns true


    {1,2,3}
    {3,6,4,8}
    returns false

    how wouls i then fix the program??

    thanks
     
  20. Nov 22, 2004 #19
    Why are you calling the function, 'isDifferent'? Shouldn't it be called 'isaSubsetOf', or 'isContainedIn' if that's what you want it to do.
     
  21. Nov 22, 2004 #20
    yes, sorry, its just a generic method name......
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?



Similar Discussions: A.length() and b.length() java
  1. Java's Scanner (Replies: 6)

  2. Java Help (Replies: 7)

  3. Java help (Replies: 15)

  4. Java q (Replies: 3)

  5. Java problem. (Replies: 4)

Loading...