1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Rep Power

    IndexOf and lastIndexOf

    Hey guys, I'm trying to run a for loop through a specific section of a vector using indexOf and lastIndexOf. The problem is that the vector is filled with a bunch of "User" classes, and that I'm differentiating between them by a string inside the class, "advisor." This makes using indexOf and lastIndex of tricky, I feel like it should be possible to do but I have no idea what the notation would look like. I wrote this code, not expecting it to work (it didn't), but here it is anyway:
    	for(int start = user.indexOf(User.advisor=aFind); start<(user.lastIndexOf(User.advisor=aFind)+1); start++)
    everything should be self explanatory, aFind is a String I defined inside my application, its value is completely irrelevant for now. If this is actually possible could someone walk me through the proper notation? Thanks. Oh, the vector has been bubble sorted based on the advisor string already, so all the Users with the value for advisor I'm looking for are in between the first and last index of Users with the specified advisor string.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Nov 2004
    Washington DC
    Rep Power
    I'm not too clear on what you're trying to accomplish. Syntax wise, Iterating through the list and acting on Users with certain advisers should be similar to how you did the bubble sort.

    It sounds like you need to find the first occurrence of User where advisor.equals(aFind) . Because you know your User objects are sorted by adviser, you can iterate until !advisor.equals(aFind) and you'll have the last index of value.
    Open for extension, closed for modification

IMN logo majestic logo threadwatch logo seochat tools logo