Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: how to find an element in an array

  1. #1
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default how to find an element in an arrayList

    Hi guys,

    Im trying to write a very small and simple method that will find an element from a arrayList and return it. the list contains instances of a class called People. I tried using the indexOf method to find the index of the desired element and then the get() method to return the element using the previously found index.

    I just keep getting the java.lang.ArrayIndexOutOfBoundsException: -1 error.


    the code I have is:

    public Names findPerson(String aString)
       {
          int personIndex = people.lastIndexOf(aString);
          return people..get(personIndex);
       }

    any help is much appreciated.
    Last edited by humdinger; April 9th, 2010 at 04:15 AM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: how to find an element in an array

    any of the indexOf methods (first, last, etc.) return -1 if the string was not found.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    humdinger (April 9th, 2010)

  4. #3
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: how to find an element in an array

    ok thankyou for the advice, i know the string is in there so I will do some testing on this later. Does anyone have any ideas?

  5. #4
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: how to find an element in an array

    Have you considered using Hashtables?

  6. #5
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: how to find an element in an array

    Hi, i thought about tthat but as I only have an arrayList with all the People objects stored in it Im not sure it would work as I have nothing to use as keys. I will definately look into it in more detail later though. Im unable to do it now as im at work & not with my computer. Thankyou for the advice

  7. #6
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: how to find an element in an array

    Doesn't your People object has a id? something along the way of peopleId.

    I'm using a Hastable with User objects in it, and my userid is the key in the Hashtable, that way I can easy find users based on their id

  8. The Following User Says Thank You to Bryan For This Useful Post:

    humdinger (April 9th, 2010)

  9. #7
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: how to find an element in an array

    ok great il give it a try when i get home
    thanks again
    Last edited by humdinger; April 9th, 2010 at 09:46 AM.

  10. #8
    Member
    Join Date
    Nov 2009
    Posts
    30
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Default Re: how to find an element in an array

    it works!!! thankyou Brian, I cant believe I didnt think of that before! silly me

  11. #9
    Member
    Join Date
    Apr 2010
    Location
    The Hague, Netherlands
    Posts
    91
    Thanks
    3
    Thanked 10 Times in 10 Posts

    Default Re: how to find an element in an array

    No problem, I just started learning Java and also had to deal with the same problem that you had.

    I first solved it by making my own get(id) and then iterate through the array and comparing the id with the object->getID(). But I refused to believe Java didn't had a better solution for this so I went through the documentation and found it

Similar Threads

  1. FAQ: find variable in an array of objects
    By dalek in forum Object Oriented Programming
    Replies: 1
    Last Post: April 5th, 2010, 12:40 PM
  2. new arrayList element overwrites all previous
    By twinkletoes in forum Object Oriented Programming
    Replies: 2
    Last Post: April 2nd, 2010, 07:45 AM
  3. Add white spices before the String element
    By bookface in forum Java Theory & Questions
    Replies: 1
    Last Post: March 23rd, 2010, 08:50 PM
  4. [SOLVED] theory behind testing each element of an array.
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: February 5th, 2010, 09:04 AM
  5. How to extract a particular element details which has more references ???
    By j_kathiresan in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 31st, 2009, 01:11 AM