Results 1 to 9 of 9
  1. #1
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    101
    Rep Power
    0

    Default Length of an Array: length() or just length ?

    Please consider a code snippet below:

    Java Code:
    public static int[] merge(final int[] arrayA, final int[] arrayB) {
            for (int iterB = arrayB.length - 1, iterASelect = arrayB.length - 1, iterAInsert =
                    arrayA.length - 1; iterB >= 0;) {

    I have seen that many people use only "length" to find out the length of an array while the correct way to find out the length is by using "length()" method.

    Could anyone please explain me why in the above code snippet,
    Java Code:
    iterB = arrayB. length
    is used. Shouldn't it be
    Java Code:
    iterB = arrayB.length()
    ?


    Thanks in advance !

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    Default Re: Length of an Array: length() or just length ?

    Arrays don't have methods; to get the length of an array you have to refer to its array.length member.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Length of an Array: length() or just length ?

    Because the answer is so obvious. Anyone will tell you to consult the java API documentation

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: Length of an Array: length() or just length ?

    You should first try to write the code using length() to access the array length. And you'll know which one is correct.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Length of an Array: length() or just length ?

    Quote Originally Posted by wsaryada View Post
    You should first try to write the code using length()
    Um, length is a member of an array. Not a method.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    7

    Default Re: Length of an Array: length() or just length ?

    Yes, we all know that. Just want the OP see for himself what will happen when he use length() instead of length.

  7. #7
    Jack_Tauson_Sr is offline Senior Member
    Join Date
    Nov 2011
    Posts
    101
    Rep Power
    0

    Default Re: Length of an Array: length() or just length ?

    Thanks everyone ! I was getting confused with the length() method of String actually.

  8. #8
    tamilarasi is offline Member
    Join Date
    Nov 2012
    Location
    India
    Posts
    70
    Rep Power
    0

    Default Re: Length of an Array: length() or just length ?

    I think this tutorial will be useful to you...
    Strings (The Java™ Tutorials > Learning the Java Language > Numbers and Strings)

    you just read string length topic in this document..
    Regards
    Android developer at Trinay Technology Solutions,http://www.trinaytech.com,5705750475

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Length of an Array: length() or just length ?

    Quote Originally Posted by wsaryada View Post
    Yes, we all know that. Just want the OP see for himself what will happen when he use length() instead of length.
    Oops. Sincere apologies.

    Note to self: don't post responses when in too much of a rush to read the entire thread.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Length of an array !
    By Jack_Tauson_Sr in forum New To Java
    Replies: 6
    Last Post: 12-26-2012, 04:22 AM
  2. Difference between length() and length
    By mitra in forum New To Java
    Replies: 7
    Last Post: 07-28-2011, 12:46 AM
  3. Trying to display the length of the array using tags
    By Ms.Ranjan in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-03-2010, 12:15 AM
  4. java Array.length property
    By X-ion in forum New To Java
    Replies: 0
    Last Post: 04-07-2010, 08:45 AM
  5. Using reflection to check array type and length
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 08:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •