Results 1 to 8 of 8
Like Tree3Likes
  • 1 Post By JosAH
  • 1 Post By jim829
  • 1 Post By Norm

Thread: Specific question

  1. #1
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Specific question

    Given this statement
    Java Code:
    position[exams[i].getID()]
    .. if, for example, exams.getID() returns 88, so position[88] would return a postion of 88 in an array, correct?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Specific question

    That could work.
    Did you try it? What happened?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Re: Specific question

    this line gives me OutOfBoundException

  4. #4
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default Re: Specific question

    so, position[88] gives address in an array, doesn't it?

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Specific question

    Unravel your expression a bit; your expression is equivalent to:

    Java Code:
    int index= exams[i].getID();
    position[index]
    If you get an IndexOutOfBounds exception, the index value is < 0 or >= position.length; otherwise, the last part gives the index-th element in the position array.

    kind regards,

    Jos
    ssk2659 likes this.
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Specific question

    If arrays were indexed starting at 1, then index 1 would get the first element and
    index 10 would get the 10th element. But in Java, arrays are 0 based. So index[0] gets the first
    element, index[1] gets the second element and index[9] gets the 10th element. For any array of
    length N, the maximum index is N-1. This is also true for ArrayList and other List implementations.
    However, it does not have to be that way. The documentation should state whether it is true or not.

    Regards,
    Jim
    ssk2659 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Specific question

    so, position[88] gives address in an array, doesn't it?
    That would be OK in an array with 89 or more elements.
    ssk2659 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Specific question

    Quote Originally Posted by ssk2659 View Post
    Specific question
    Please go through the Forum Rules, particularly the third paragraph.

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

Similar Threads

  1. Replies: 14
    Last Post: 02-09-2015, 02:08 AM
  2. How to allow specific letters only?
    By ctmarco3 in forum New To Java
    Replies: 7
    Last Post: 04-01-2011, 04:41 AM
  3. Specific Functions for URL's
    By lyric0n in forum Java Servlet
    Replies: 3
    Last Post: 04-06-2010, 12:34 PM
  4. Replies: 2
    Last Post: 11-08-2008, 10:40 AM

Posting Permissions

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