Results 1 to 5 of 5
  1. #1
    Gamerpro is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default Return Smallest Index (Solved)!

    Up front, I want to thank anyone for helping me with this task.

    I was recently given this task:
    Create a method called findElementIndex, which takes an integer and an integer array as input parameters, and returns the smallest index where that value appears. If the value is not in the array, the method should return -1.

    Example:
    value: 3 theArray:{10, 3, 6, 3, 8, 10} ==> 1
    value:10 theArray:{11, 3, 6, 7, 9, 60} ==> -1

    So far, I have this:

    int index = 0;
    for(int i = 0; i < theArray.length; i++){
    if (theArray[i] == value) {
    index = i;
    }
    else if (theArray[i] != value){
    index = -1;
    }
    }
    return index;
    Last edited by Gamerpro; 02-25-2009 at 02:28 AM.

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    two problems with your algorithm:

    1. it iterates through the whole loop. even if you find the first incidence of the input 'value', it will keep going.

    2. it iterates through the WHOLE loop, setting a new value for 'index' each time around, meaning your final value will either be the last index or -1.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default

    OK.. you seem to be on the correct track... some corrections and minor tweaks.
    • No need to for the else... let the for loop run it's course.
    • No need for index variable ...

    Java Code:
    for(int i = 0; i < theArray.length; i++){
    if (theArray[i] == value) {
    [COLOR="Blue"][B]return i; [/B][/COLOR]
    }
    }
    [COLOR="Blue"][B]return = -1; //value not found [/B][/COLOR][B][/B]
    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    Gamerpro is offline Member
    Join Date
    Feb 2009
    Posts
    3
    Rep Power
    0

    Default Oh.....

    O.K. Thanks a lot, I see what I was doing wrong; I was trying the code you gave me and I kept on getting errors because I didn't notice the return = -1 in your above post, it was supposed to be return -1, silly me! I appreciate the help from both of you!
    Last edited by Gamerpro; 02-25-2009 at 02:33 AM.

  5. #5
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    7

    Default welcome

    sure, no problem...

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  2. Replacing at an index
    By bugger in forum New To Java
    Replies: 2
    Last Post: 01-29-2008, 06:33 AM
  3. Finding largest and smallest integer
    By mlhazan in forum New To Java
    Replies: 2
    Last Post: 01-12-2008, 10:30 PM
  4. z-Index problem
    By mjdousti in forum AWT / Swing
    Replies: 1
    Last Post: 12-29-2007, 01:34 AM
  5. Getting smallest possible Double value
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-06-2007, 02: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
  •