Results 1 to 4 of 4
  1. #1
    ranjanarr is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default Array without using Array.length

    can any one tell me how to find length of an array (int or char) without using array.length method in Java?

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ranjanarr View Post
    can any one tell me how to find length of an array (int or char) without using array.length method in Java?
    It would be silly not to use the length field, but I've seen sillier things so here goes:

    Java Code:
    public static int length(int[] array) {
       int i;
       try {
          for (i= 0; i < Integer.MAX_VALUE; i++) {
             int dummy= array[i];
          }
          return i; /* very unlikely to reach here */
       }
       catch (ArrayIndexOutOfBoundsException aioobe) {
          return i;
       }
    }
    kind regards,

    Jos

  3. #3
    ranjanarr is offline Member
    Join Date
    May 2010
    Posts
    2
    Rep Power
    0

    Default

    Hello Jos,

    Thank you for reply, how will Integer.MAX_VALUE help find length of random array here, u are passing array as parameter to function but what is Integer.MAX_VALUE doing in this code. thanks

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,520
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ranjanarr View Post
    Hello Jos,

    Thank you for reply, how will Integer.MAX_VALUE help find length of random array here, u are passing array as parameter to function but what is Integer.MAX_VALUE doing in this code. thanks
    Have you read the API documentation for the Integer class? You should have and preferably you should've bookmarked that Sun site for handy reference; you need it (I need it too, everybody who wants to program in Java needs that documentation). Here's the link.

    kind regards,

    Jos
    Last edited by JosAH; 05-23-2010 at 09:21 AM.

Similar Threads

  1. java Array.length property
    By X-ion in forum New To Java
    Replies: 0
    Last Post: 04-07-2010, 08:45 AM
  2. Array length and printing out uninitialized array.
    By nicolek808 in forum New To Java
    Replies: 4
    Last Post: 09-10-2009, 09:12 AM
  3. Replies: 1
    Last Post: 03-31-2009, 06:40 AM
  4. 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
  5. Using reflection to check array type and length
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:42 PM

Tags for this Thread

Posting Permissions

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